Steven
Steven

Reputation: 697

Save multiple queries to sql database

I am trying to save multiple queries into a database on two different tables. Below is the code that I have tried to no avail. firsttable.a is the same as secondtable.id, con holds the connection info, and everything saves perfectly with one query. Is there something I am missing here?

if(empty($id)){
$uuid = uniqid();
$query = "INSERT INTO firsttable (`id`, `a`, `b`, `uuid`) VALUES (NULL, '$a', '$b', '$uuid')";

$query2 = "INSERT INTO secondtable (`id`, `c`, `d`, `uuid`) VALUES (NULL, '$c', '$d', '$uuid')";
}else{
$query = "UPDATE `firsttable` SET `id` = '$id', `a` = '$a', `b` = '$b', `uuid` = '$uuid' WHERE `id` = $id";

$query2 = "Update INTO secondtable SET `id` = '$a', `c` = '$c', `d` = '$d',

if(!mysqli_multi_query($this->_con, $query;$query2)){
                    throw new Exception(  mysqli_error($this->_con) );

Upvotes: 0

Views: 97

Answers (1)

jbafford
jbafford

Reputation: 5668

mysql_multi_query takes two arguments: the database connection, and a single string.

You need to concatenate your two queries together as a string:

mysqli_multi_query($this->con, $query1 . ';' . $query2);

or what you were probably trying to do:

mysqli_multi_query($this->con, "$query1;$query2");

From the php documentation on how to retrieve the result sets for the subsequent queries:

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

The first example shows how it all works.

In your example, though, the correct syntax is UPDATE tablename ..., not UPDATE INTO tablename ....

Upvotes: 1

Related Questions