Jump to content
New Reality: Ads For Members ×

same table delete and insert


shaddf

Recommended Posts

Yes.

 

Here's one way; $Q is an array of queries.

 

function transaction ($con, $Q){

mysqli_query( $con, "START TRANSACTION" );

for ( $i = 0; $i < count( $Q ); $i++ ) {
if ( !mysqli_query ( $con, $Q[$i] )) {
echo 'Error! Info: <' . mysqli_error ($con) . '> Query: <' . $Q[$i] . '>';
break;
}
}

if ( $i == count ( $Q )) {
mysqli_query( $con, "COMMIT" );
return 1;
} else {
mysqli_query( $con, "ROLLBACK" );
return 0;
}
}

Yes you can enclose whatever you want in a transaction.  That is the point of them -- and they are of course not limited to one table.  You could insert in table A, Delete from Table B, and update table C, all inside one transaction.

 

The important thing to know is that with mysql your tables need to be of an engine type that supports transactions.  Most people have traditionally used Innodb engine tables for this requirement.

Yes.

 

Here's one way; $Q is an array of queries.

 

function transaction ($con, $Q){

mysqli_query( $con, "START TRANSACTION" );

for ( $i = 0; $i < count( $Q ); $i++ ) {
if ( !mysqli_query ( $con, $Q[$i] )) {
echo 'Error! Info: <' . mysqli_error ($con) . '> Query: <' . $Q[$i] . '>';
break;
}
}

if ( $i == count ( $Q )) {
mysqli_query( $con, "COMMIT" );
return 1;
} else {
mysqli_query( $con, "ROLLBACK" );
return 0;
}
}

both answers are good

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.