Jump to content

implement commit() in php 4


orange08

Recommended Posts

can anyone here kindly provide me with an example of implementing commit() in php 4?
If you mean a database commit, then you also need to be using a transactional database. But assuming you are, then you'd simply execute it as a standard database query.

 

$SQLquery = 'START TRANSACTION';
$result = mysql_query($SQLquery);
$SQLquery = 'UPDATE table SET column=1 WHERE type=2';
$result = mysql_query($SQLquery);
if (!$result) {
   $SQLquery = 'ROLLBACK';
   $result = mysql_query($SQLquery);
} else {
   $SQLquery = 'COMMIT';
   $result = mysql_query($SQLquery);
}

  • 1 month later...

can anyone here kindly provide me with an example of implementing commit() in php 4?
If you mean a database commit, then you also need to be using a transactional database. But assuming you are, then you'd simply execute it as a standard database query.

 

$SQLquery = 'START TRANSACTION';
$result = mysql_query($SQLquery);
$SQLquery = 'UPDATE table SET column=1 WHERE type=2';
$result = mysql_query($SQLquery);
if (!$result) {
   $SQLquery = 'ROLLBACK';
   $result = mysql_query($SQLquery);
} else {
   $SQLquery = 'COMMIT';
   $result = mysql_query($SQLquery);
}

 

thanks for the code, as we start the code from 'start transaction', need to close transaction or not after the query is executed?

Both COMMIT and ROLLBACK queries end current transaction.

 

so, meant that either COMMIT or ROLLBACK must be called to complete the code?

 

$SQLquery = 'START TRANSACTION';
$result = mysql_query($SQLquery);
$SQLquery = 'UPDATE table SET column=1 WHERE type=2';
$result = mysql_query($SQLquery);
if (!$result) {
   $SQLquery = 'ROLLBACK';
  [color=red] $result = mysql_query($SQLquery);[/color]
} else {
   $SQLquery = 'COMMIT';
   $result = mysql_query($SQLquery);
}

 

i can't understand why the line of code that in red color is needed...

in this case, if $result success or fail, then no more COMMIT or ROLLBACK is called...,right?

$SQLquery = 'START TRANSACTION';
$result = mysql_query($SQLquery);
$SQLquery = 'UPDATE table SET column=1 WHERE type=2';
$result = mysql_query($SQLquery);
if (!$result) {
   $SQLquery = 'ROLLBACK';
   $result = mysql_query($SQLquery);
} else {
   $SQLquery = 'COMMIT';
   $result = mysql_query($SQLquery);
}

 

in what case, !$result will be met? if my SQL query WHERE clause can't be reached, will it causing !$result?

 

can give example when !$result will happen? thanks!

in what case, !$result will be met? if my SQL query WHERE clause can't be reached, will it causing !$result?

 

can give example when !$result will happen? thanks!

$result can be a boolean false (fail) if there is an error in the SQL statement, or if the database cannot be accessed.

 

If no records match the where clause, and so nothing is updated, this is not an error.

You can test for this separately using the mysql_affected_rows() function

 

in what case, !$result will be met? if my SQL query WHERE clause can't be reached, will it causing !$result?

 

can give example when !$result will happen? thanks!

$result can be a boolean false (fail) if there is an error in the SQL statement, or if the database cannot be accessed.

 

If no records match the where clause, and so nothing is updated, this is not an error.

You can test for this separately using the mysql_affected_rows() function

 

ok, thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.