Can mysqli Prepare Multiple Queries in One Statement?
In mysqli, a prepared statement is designed to execute a single MySQL query. While it is not possible to prepare multiple queries in a single statement, you can create multiple prepared statements in different variables.
To do so, use the following approach:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)"); $stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");
You can then execute these statements independently. For instance:
$stmtUser->bind_param("ssss", $id, $username, $pw, $email); $stmtUser->execute(); $stmtProc->bind_param("ss", $id, $idp); $stmtProc->execute();
If you require strict execution of both queries simultaneously, you should consider using transactions. Transactions ensure that either both queries succeed or both fail.
Finally, remember that a "call to member function on a non-object" error typically indicates a failure in the prepare() statement, requiring you to inspect it for errors.
The above is the detailed content of Can MySQLi Prepared Statements Handle Multiple Queries Simultaneously?. For more information, please follow other related articles on the PHP Chinese website!