Jump to content

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

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.