Can MySQLi Prepare Multiple Queries in One Statement?
In MySQLi, there are limitations on preparing multiple queries in a single statement. A prepared statement can only execute one MySQL query. However, you can prepare as many statements as you need, storing them in different variables. Here's an example:
<code class="php">$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)"); $stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");</code>
Later, you can execute these statements independently:
<code class="php">$stmtUser->execute(); $stmtProc->execute();</code>
If you require both statements to be executed successfully or not at all, consider using transactions.
Error Handling:
If you encounter the error "Call to a member function bind_param() on a non-object," it indicates that the prepare() call failed, resulting in an invalid prepared statement object. In such cases, it's essential to inspect the prepare() statement for potential errors.
The above is the detailed content of Can MySQLi Prepare Multiple Queries in One Statement?. For more information, please follow other related articles on the PHP Chinese website!