Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli: can it prepare multiple queries in one statement?

Tags:

I would like to know if i can prepare one mysqli statement that executes multiple queries:

mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...); or  mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...); and after all mysqli->bind_param("sssss", 1, 2, 3, 4, 5); 

In that way it make error: Call to a member function bind_param() on a non-object in...

$stmt = $sql->getQueryPrepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?); INSERT INTO process (id_user, idp) VALUES (?,?);");  $stmt->bind_param("ssssss",$id, $username, $pw, $email, $id, $idp);  $stmt->execute(); $stmt->close();  
like image 388
Donovant Avatar asked Jul 24 '12 14:07

Donovant


People also ask

How do I run multiple MySQL queries at once?

MySQL UNION operator To combine result set of two or more queries using the UNION operator, these are the basic rules that you must follow: First, the number and the orders of columns that appear in all SELECT statements must be the same. Second, the data types of columns must be the same or compatible.

Can we use single prepared statement for multiple queries?

Statement interface can be used to execute static SQL queries whereas PreparedStatement interface is used to execute dynamic SQL queries multiple times. In this example, the java.

How run multiple MySQL queries in PHP?

Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.


2 Answers

A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables:

$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)"); $stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);"); 

And then execute them later. If you want to ensure that neither one is ever run unless both are able to run, then you need to look into transactions, like Thomas said.

Also, a general tip: "call to member function on a non-object" is the standard error you get when prepare() fails and so $stmt isn't actually a prepared statement object. It usually means you need to look for an error in your prepare() statement rather than anything later.

like image 57
octern Avatar answered Sep 25 '22 08:09

octern


No, a single call to the mysqli prepare() function cannot prepare multiple queries at once. You can, however, prepare more than one query for execution by using different variables. The documentation for this function is available here.

It also looks like you are trying to setup a transaction, which is a different question than you asked. If that's what you really want to know, then you'll need to provide more information about your database setup and probably more specifics about the use case you are trying to solve.

like image 24
Thomas Avatar answered Sep 25 '22 08:09

Thomas