Jump to content

multiple MYSql queries


M.O.S. Studios

Recommended Posts

I have to put data into multiple tables in a MYSql database. I would like to know if there is a php function (or class) that does a batch query; and only adds the data only if every query is successful.

 

the only thing i can think of is adding a custom function that does each query one at a time, and undoes them if a problem is found.

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/240542-multiple-mysql-queries/
Share on other sites

You could just do something like this....

 

<?php

function performMySQLBatchJobs($jobArray) {

$jobsPerformed = 0;
$isValidResult = true;

foreach($jobArray as $job) {

if($isValidResult) {
    $isValidResult = mysql_query($job);
    $jobsPerformed++;
} else {
break;
}
}

return ($jobsPerformed - 1);

}

$batchJobs = array("INSERT INTO `test` (`col`) VALUES 'val';", "INSERT INTO `test` (`col`) VALUES 'val';", "INSERT INTO `test` (`col`) VALUES 'val';");

$numberOfJobsDone = performMySQLBatchJobs($batchJobs);

echo $numberOfJobsDone." Out of ".count($batchJobs)." Performed.";

?>

 

In this case you would also need to delete your queries if they are chained like that. Your queries shouldn't break though, that's poor programming. Perform checks before executing the query.

I have to put data into multiple tables in a MYSql database. I would like to know if there is a php function (or class) that does a batch query; and only adds the data only if every query is successful.

 

the only thing i can think of is adding a custom function that does each query one at a time, and undoes them if a problem is found.

 

 

thanks in advance

 

you probably want to use TRANSACTIONS  and COMMIT or ROLLBACK all if something is wrong (obviously you must code the necessary validations).... Stored Procedures or Functions and probably EVENTS could be useful for you too.... everything well documented in the Mysql Manual

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.