Home  >  Article  >  Database  >  Can MySQLi Prepare Multiple Queries in One Statement?

Can MySQLi Prepare Multiple Queries in One Statement?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-28 17:44:02288browse

Can MySQLi Prepare Multiple Queries in One Statement?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn