mysqli Query Preparation with Multiple Queries
Multi-query preparation is not supported by mysqli's prepared statement feature. A prepared statement in mysqli is limited to executing a single MySQL query.
Alternative Approach:
If you need to execute multiple queries in sequence, you can create separate prepared statements for each query:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)"); $stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?)");
Transaction-Based Approach:
Alternatively, you can use transactions to ensure that either both queries are executed or neither is executed:
$sql->begin_transaction(); // Execute both queries if (!$stmtUser->execute() || !$stmtProc->execute()) { $sql->rollback(); } else { $sql->commit(); }
Error Debugging:
If you encounter an "call to member function on a non-object" error, it indicates that the preparation of the statement failed. Check your prepare() statement carefully for any errors.
The above is the detailed content of Can MySQLi Prepared Statements Handle Multiple Queries?. For more information, please follow other related articles on the PHP Chinese website!