"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Can I Prepare Multiple Queries in a Single MySQLi Statement?

Can I Prepare Multiple Queries in a Single MySQLi Statement?

Published on 2024-11-08
Browse:196

Can I Prepare Multiple Queries in a Single MySQLi Statement?

Preparing Multiple Queries in a Single MySQLi Statement

It is not possible to prepare multiple queries in a single MySQLi statement. Each mysqli_prepare() call can only prepare a single query.

Alternative Approach for Executing Multiple Queries

If you need to execute multiple queries in one go, you can create and execute separate mysqli_prepare() 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 (?,?);");

$stmtUser->bind_param("ssss", $id, $username, $pw, $email);
$stmtProc->bind_param("ss", $id, $idp);

$stmtUser->execute();
$stmtProc->execute();

$stmtUser->close();
$stmtProc->close();

Handling Errors

The error "Call to a member function on a non-object" is typically encountered when prepare() fails. This is usually due to an error in the prepared statement itself, not in subsequent code.

Transaction Support

If you want to ensure that multiple queries are executed together or not at all, MySQLi supports transactions. You can use the mysqli_begin_transaction() and mysqli_commit() functions to control transaction boundaries.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3