MySQLi
在线手册:中文 英文
PHP手册

mysqli::multi_query

mysqli_multi_query

(PHP 5)

mysqli::multi_query -- mysqli_multi_queryPerforms a query on the database

说明

面向对象风格

bool mysqli::multi_query ( string $query )

过程化风格

bool mysqli_multi_query ( mysqli $link , string $query )

Executes one or multiple queries which are concatenated by a semicolon.

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().

参数

link

仅以过程化样式:由 mysqli_connect()mysqli_init() 返回的链接标识。

query

The query, as a string.

Data inside the query should be properly escaped.

返回值

Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.

范例

Example #1 mysqli::multi_query() example

面向对象风格

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

$query  "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        
/* store first result set */
        
if ($result $mysqli->store_result()) {
            while (
$row $result->fetch_row()) {
                
printf("%s\n"$row[0]);
            }
            
$result->free();
        }
        
/* print divider */
        
if ($mysqli->more_results()) {
            
printf("-----------------\n");
        }
    } while (
$mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

过程化风格

<?php
$link 
mysqli_connect("localhost""my_user""my_password""world");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

$query  "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if (mysqli_multi_query($link$query)) {
    do {
        
/* store first result set */
        
if ($result mysqli_store_result($link)) {
            while (
$row mysqli_fetch_row($result)) {
                
printf("%s\n"$row[0]);
            }
            
mysqli_free_result($result);
        }
        
/* print divider */
        
if (mysqli_more_results($link)) {
            
printf("-----------------\n");
        }
    } while (
mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);
?>

以上例程的输出类似于:

my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer

参见


MySQLi
在线手册:中文 英文
PHP手册
PHP手册 - N: Performs a query on the database

用户评论:

crmccar at gmail dot com (13-Oct-2011 02:40)

I'd like to reinforce the correct way of catching errors from the queries executed by multi_query(), since the manual's examples don't show it and it's easy to lose UPDATEs, INSERTs, etc. without knowing it.

$mysqli->next_result() will return false if it runs out of statements OR if the next statement has an error. Therefore, it's important to check for errors when the loop ends. Also, I believe it's useful to know when and where the loop broke, so consider the following code:

<?php
$statements
= array("INSERT INTO tablename VALUES ('1', 'one')", "INSERT INTO tablename VALUES ('2', 'two')");
if (
$mysqli->multi_query(implode(';', $statements))) {
   
$i = 0;
    do {
       
$i++;
    } while (
$mysqli->next_result());
}
if (
$mysqli->errno) {
    echo
"Batch execution prematurely ended on statement $i.\n";
   
var_dump($statements[$i], $mysqli->error);
}
?>

The IF statement on the multi_query() call checks the first result, because next_result() starts at the second.

lcruz at humansoft dot pt (29-Aug-2011 10:16)

Please note that MySQL does not support Transactions on DDL statements.

<?php
$link
= mysqli_connect('localhost', 'user', 'pw', 'db');

$script = "INSERT INTO T1('pk', 'val') VALUES('1', 'A');";
$script .= "INSERT INTO T1('pk', 'val') VALUES('2', 'B');"
$script .= "INSERT INTO T1('pk', 'val') VALUES('3', 'C');"
$script .= "ALTER TABLE T1 ADD COLUMN `foo` INT(1) NOT NULL;";
$script .= "UPDATE T1 SET val = 'D' where pkkkkkkkkkkkk = '1'"; // Note that this statement will force a SQL Error since column pkkkkkkkkkkkk doesn't exist

mysqli_autocommit($link, false); // set autocommit to false

mysqli_multi_query($link, $script); // execute statements

mysqli_rollback($link); // rollback all statements
?>

I would expect that the entire set of statements would not occur since the last update would generate an error. However after the code execution you'll see the values on table T1.

I've spent a couple of days to find that this behavior is normal in MySQL since the ALTER TABLE statement automatically commits the transaction. See more in http://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html

Anonymous (21-May-2011 03:25)

If your second or late query returns no result or even if your query is not a valid SQL query, more_results(); returns true in any case.

jcn50 (09-Mar-2011 01:09)

WATCH OUT: if you mix $mysqli->multi_query and $mysqli->query, the latter(s) won't be executed!

<?php
// BAD CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK
$mysqli->query(" SQL statement #1 ; ") // not executed!
$mysqli->query(" SQL statement #2 ; ") // not executed!
$mysqli->query(" SQL statement #3 ; ") // not executed!
$mysqli->query(" SQL statement #4 ; ") // not executed!
?>

The only way to do this correctly is:

<?php
// WORKING CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK
while ($mysqli->next_result()) {;} // flush multi_queries
$mysqli->query(" SQL statement #1 ; ") // now executed!
$mysqli->query(" SQL statement #2 ; ") // now executed!
$mysqli->query(" SQL statement #3 ; ") // now executed!
$mysqli->query(" SQL statement #4 ; ") // now executed!
?>

luka8088 at owave dot net (20-Sep-2010 06:17)

if you don't iterate through all results you get "server has gone away" error message ...

to resolve this, in php 5.2 it is enough to use

<?php
 
// ok for php 5.2
 
while ($mysqli->next_result());
?>

to drop unwanted results, but in php 5.3 using only this throws

mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method

so it should be replaced with

<?php
 
// ok for php 5.3
 
while ($mysqli->more_results() && $mysqli->next_result());
?>

I also tried but failed:

<?php

 
// can create infinite look in some cases
 
while ($mysqli->more_results())
   
$mysqli->next_result();

 
// also throws error in some cases
 
if ($mysqli->more_results())
    while (
$mysqli->next_result());

?>

Shawn Pyle (15-Apr-2010 05:29)

Be sure to not send a set of queries that are larger than max_allowed_packet size on your MySQL server. If you do, you'll get an error like:
Mysql Error (1153): Got a packet bigger than 'max_allowed_packet' bytes

To see your MySQL size limitation, run the following query: show variables like 'max_allowed_packet';

or see http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html

Miles (22-Jun-2009 06:47)

You can use prepared statements on stored procedures.

You just need to flush all the subsequent result sets before closing the statement... so:

$mysqli_stmt = $mysqli->prepare(....);

... bind, execute, bind, fetch ...

while($mysqli->more_results())
{
    $mysqli->next_result();
    $discard = $mysqli->store_result();
}

$mysqli_stmt->close();

Hope that helps :o)

raye1010 at yahoo dot com dot hk (29-Jan-2009 06:56)

This is my point of view:

Actually when calling $mysqli->next_result(), MySQL server will try to prepare resources for storing resultset. If you want to multi query for stored procedures, it's better to use $mysqli->use_result()->close() to close the resources. For functions, it's better to use $mysqli->store_result()->free() since there's resultset returned. Otherwise, an error of "Commands out of sync" will be rasied.

Here's the code I test:

<?php
$mysql
= new mysql('localhost', 'user', 'pw', 'db');

$query = 'show tables;';
$query.= 'show tables;';
$query.= 'select * from dummy_table;';
$query.= 'show tables;'; // this statement will be ignored as the dummy_table doesn't exist and the loop should quit

$mysqli->multi_query($query);
do {
 
$mysqli->use_result()->close();
  echo
"Okay\n";
} while (
$mysqli->next_result());

if (
$mysqli->errno) {
  echo
"Stopped while retrieving result : ".$mysqli->error;
}

?>
Returns :
Okay
Okay
Stopped while retrieving result : Table 'db.dummy_table' doesn't exist

jparedes at gmail dot com (27-Aug-2008 05:05)

It's very important that after executing mysqli_multi_query you have first process the resultsets before sending any another statement to the server, otherwise your
socket is still blocked.

Please note that even if your multi statement doesn't contain SELECT queries, the server will send result packages containing errorcodes (or OK packet) for single statements.

undefined(AT)users(DOT)berlios(DOT)de (04-Mar-2008 12:13)

mysqli_multi_query handles MySQL Transaction on InnoDB's :-)

<?php

$mysqli 
= mysqli_connect( "localhost", "owner", "pass", "db", 3306, "/var/lib/mysql/mysql.sock" );

$QUERY = <<<EOT
START TRANSACTION;
SELECT @lng:=IF( STRCMP(`main_lang`,'de'), 'en', 'de' )
FROM `main_data` WHERE  ( `main_activ` LIKE 1 ) ORDER BY `main_id` ASC;
SELECT `main_id`, `main_type`, `main_title`, `main_body`, `main_modified`, `main_posted`
FROM `main_data`
WHERE ( `main_type` RLIKE "news|about" AND `main_lang` LIKE @lng AND `main_activ` LIKE 1 )
ORDER BY `main_type` ASC;
COMMIT;
EOT;

$query = mysqli_multi_query( $mysqli, $QUERY ) or die( mysqli_error( $mysqli ) );

if(
$query )
{
  do {
    if(
$result = mysqli_store_result( $mysqli ) )
    {
     
$subresult = mysqli_fetch_assoc( $result );
      if( ! isset(
$subresult['main_id'] ) )
        continue;

      foreach(
$subresult AS $k => $v )
      {
       
var_dump( $k , $v );
      }
    }
  } while (
mysqli_next_result( $mysqli ) );
}

mysqli_close( $mysqli );

?>

mjmendoza at grupzero dot tk (30-Nov-2006 06:47)

I was developing my own CMS and I was having problem with attaching the database' sql file. I thought mysqli_multi_query got bugs where it crashes my MySQL server. I tried to report the bug but it showed that it has duplicate bug reports of other developers. To my surprise, mysqli_multi_query needs to bother with result even if there's none.

I finally got it working when I copied the sample and removed somethings. Here is what it looked liked

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
  
printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

$query  = "CREATE TABLE....;...;... blah blah blah;...";

/* execute multi query */
if (mysqli_multi_query($link, $query)) {
   do {
      
/* store first result set */
      
if ($result = mysqli_store_result($link)) {
          
//do nothing since there's nothing to handle
          
mysqli_free_result($result);
       }
      
/* print divider */
      
if (mysqli_more_results($link)) {
          
//I just kept this since it seems useful
           //try removing and see for yourself
      
}
   } while (
mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);
?>

bottom-line: I think mysql_multi_query should only be used for attaching a database. it's hard to handle results from 'SELECT' statements inside a single while loop.

sly underscore mcfly at hotmail dot com (08-Sep-2006 01:32)

Ive just had exactly the same problem as below trying to execute multiple stored procedures. I thought i might as well add how to do it the object oriented way.

Instead of putting the one statement:

<?php
$mysqli
->next_result();
?>

Put two:

<?php
$mysqli
->next_result();
$mysqli->next_result();
?>

The first statement points (possibly using the term incorrectly) you to the return value. The second one will point you to the result of the next query.

I hope this makes sense.

info at ff dot net (08-May-2006 12:02)

Note that you need to use this function to call Stored Procedures!

If you experience "lost connection to MySQL server" errors with your Stored Procedure calls then you did not fetch the 'OK' (or 'ERR') message, which is a second result-set from a Stored Procedure call. You have to fetch that result to have no problems with subsequent queries.

Bad example, will FAIL now and then on subsequent calls:
<?php
$sQuery
='CALL exampleSP('param')';
if(!
mysqli_multi_query($this->sqlLink,$sQuery))
 
$this->queryError();
$this->sqlResult=mysqli_store_result($this->sqlLink);
?>

Working example:
<?php
$sQuery
='CALL exampleSP('param')';
if(!
mysqli_multi_query($this->sqlLink,$sQuery))
 
$this->queryError();
$this->sqlResult=mysqli_store_result($this->sqlLink);

if(
mysqli_more_results($this->sqlLink))
  while(
mysqli_next_result($this->sqlLink));
?>

Of course you can do more with the multiple results then just throwing them away, but for most this will suffice. You could for example make an "sp" function which will kill the 2nd 'ok' result.

This nasty 'OK'-message made me spend hours trying to figure out why MySQL server was logging warnings with 'bad packets from client' and PHP mysql_error() with 'Connection lost'. It's a shame the mysqli library does catch this by just doing it for you.