$qry ="INSERT INTO TABLE1(COMPANY_NAME,DTTM) VALUES('comp name',now())";
$result = mysql_query($qry);
$new_id = mysql_insert_id();
//FORCE THE SECOND QRY TO NOT WORK
//TESTING ONLY
unset($new_id);
$qry2 = "INSERT INTO TABLE2 (ID,UNAME) VALUES($new_id,'')";
$result = mysql_query($qry2);
if (!$result){//QUERY DOESN'T WORK
//DELETE FIRST INSERT
$qry = "DELETE FROM TABLE1 WHERE ID=$new_id";
$result = mysql_query($qry);
echo ‘results:’.$result;
}
When I try to delete the row it does not delete. I have tried:
I'm not sure what you mean by "calculate/query" my actual app has about 5 inserts into different tables. If one insert doesn't work, I want to remove any of the previous inserts.
/**
* In PHP4 function defined in a scope of another function
* has no access to 'parent' function's scope. So proper
* closures (like in Object Pascal) are impossible.
* Operator overloading is absent also. So we can't create
* functors like in C++.
* Callback passed to preg_replace_callback does not accept
* any user-defined params.
* Thus we _have_ to define this placeholder array
* globally.
* It is used to collect the last_insert_ids of the queries
* with 'saveid' flag set.
*
* UPDATE: actually, we can use functor-like approach using
* 'call_user_func_array', 'create_function' and custom
* 'closure' class, but it would be a mess in this case.
*/
$__ids=array();
/**
* simulates transaction on non-transactional table types
* do not work for multi-row inserts
* attempts to rewind the transaction, but does not check if
* rewind was successful
*
* @param $queries array array of queries to execute
* @param $reverse_queries array array which consists of
* reverse queries
* @return bool true if transaction succeded,
* false if forced to reverse
*/
function simulate_transaction($queries, $reverse_queries){
global $mysql,$__ids;
$size = count($queries);
$reverse = false;
for($i=0; $i<$size; $i++) {
$queries[$i][0] = preg_replace_callback( //parse for %idXX macros
"/%id(\d{1,2})/",
create_function(
'$matches',
'global $__ids;
return $__ids[$matches[1]];'
),
$queries[$i][0]
);
if( !$mysql->query($queries[$i][0])
|| (!$mysql->num_rows && $queries[$i][1] == 'affect')
) {
$reverse = true;
break;
}
if( stristr($reverse_queries[$i], '%id') ) { //parse reverse query for %id macro
$reverse_queries[$i] = eregi_replace('%id',
strval( $mysql->insert_id() ),
$reverse_queries[$i]
);
}
if(@$queries[$i][2] == 'saveid') {
$__ids[$i] = $mysql->insert_id();
}
}
if($reverse)
for(--$i; $i>=0; $i--)
$mysql->query($reverse_queries[$i]);
return !$reverse;
}