[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 876
  • Last Modified:

PHP MSSQL sometimes autocommits

I have php installed on a 2003 box running IIS and MSSQL. Connection is through a ODBC driver and I'm using PHP mssql functions.

If i do a insert into one table or inserts into different tables everything is fine example and i don't have to call a commit statement.

$query = "INSERT INTO table1";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());

$query = "INSERT INTO table2";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());


but if it try to insert another row into the same table i have to call a commit statement or the second insert errors and doesn't get inserted:

Doesn't work:

$query = "INSERT INTO table1";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());

$query = "INSERT INTO table1";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());

Works:

$query = "INSERT INTO table1";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());

mssql_query("COMMIT");

$query = "INSERT INTO table1";
$result = mssql_query($query, $link)or exit(mssql_get_last_message());

I've tried finding this on the web and haven't had much success. I'm not doing any transactional sets so auto commit is fine for me. Just wanted to know why this is happening.
0
chemdry
Asked:
chemdry
  • 2
1 Solution
 
Chad SmithSoftware EngineerCommented:

SQL_AUTOCOMMIT_ON
SQLSetConnectOption(hdbc,
                    SQL_AUTOCOMMIT,
                    SQL_AUTOCOMMIT_ON);
0
 
chemdryAuthor Commented:
a little more info would be useful. Im not sure if i run that on the sql server, what hdbc is and what the scope is, does it affect all connections to all database tables? etc.

I read that odbc are by default auto commit. I checked the server properties and implicit transactions is NOT checked.

So i want to understand why this is happening.
 
0
 
chemdryAuthor Commented:
K I figured it out. It wasn't an auto commit issue after all. I was getting "the statement has terminated' and thought that was an auto commit issue.

Turned out my primary key for the table was id,user,datetime (mssql's timestamp is screwy to use unlike mysql's).

Well it was submitting two rows so quickly the date time wasn't different.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now