M.O.S. Studios Posted June 27, 2011 Share Posted June 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240542-multiple-mysql-queries/ Share on other sites More sharing options...
jaikob Posted June 27, 2011 Share Posted June 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240542-multiple-mysql-queries/#findComment-1235579 Share on other sites More sharing options...
mikosiko Posted June 27, 2011 Share Posted June 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240542-multiple-mysql-queries/#findComment-1235581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.