Query:
In MySQL, can a prepared statement execute multiple queries simultaneously? For instance:
mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...);
Or alternatively:
mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...);
Followed by:
mysqli->bind_param("sssss", 1, 2, 3, 4, 5);
Answer:
No, a prepared statement in MySQL can execute only a single query. Multiple prepared statements can be created in separate variables:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)"); $stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");
These statements can then be executed later.
To ensure that both queries are executed successfully, consider using database transactions, as suggested by Thomas.
Tip:
If you encounter the error "call to member function on a non-object" while attempting to bind parameters, it likely indicates an error in the prepare() statement itself.
The above is the detailed content of Can a Single MySQL Prepared Statement Execute Multiple Queries?. For more information, please follow other related articles on the PHP Chinese website!