Jump to content

same table delete and insert


Go to solution Solved by gizmola,

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;
}
}

Edited by dalecosp
  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.