Wednesday, 18 May 2016

php - How can i execute multiple queries using prepared statments?




I am trying to execute 2 queries at the same time. I know I can do this using mysqli::multi_query.



But is there any way I can perform a multiple queries using prepared statements?




Below is an example of my query Thanks!



$delete_all_options = "DELETE FROM option_categories WHERE item_id = ?; ";
$delete_all_options .= "DELETE FROM option_names WHERE option_category_id = ?";
$delete_stmt = $db->prepare($delete_all_options);
//Execute statement ......

Answer



You may want to use transactions. Here's an explanation for using transactions with PDO: http://php.net/manual/en/pdo.transactions.php . Transactions can be used also with the MySQLi extension, by setting MySQLi::autocommit(false) and then committing with MySQLi::commit() (with PHP 5.5+ you can also use MySQLi::begin_transaction() and other methods).




Transactions execute multiple queries "at once" and if one query fails all the transaction is reverted.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...