<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/mysqli.quickstart.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'mysqli.quickstart.multiple-statement.php',
    1 => 'Multiple Statements',
    2 => 'Multiple Statements',
  ),
  'up' => 
  array (
    0 => 'mysqli.quickstart.php',
    1 => '快速入门指南',
  ),
  'prev' => 
  array (
    0 => 'mysqli.quickstart.stored-procedures.php',
    1 => 'Stored Procedures',
  ),
  'next' => 
  array (
    0 => 'mysqli.quickstart.transactions.php',
    1 => 'API support for transactions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/mysqli/quickstart.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mysqli.quickstart.multiple-statement" class="section">
  <h2 class="title">Multiple Statements</h2>
  <p class="para">
   MySQL optionally allows having multiple statements in one statement string, 
   but it requires special handling.
  </p>
  <p class="para">
   Multiple statements or multi queries must be executed
   with <span class="methodname"><a href="mysqli.multi-query.php" class="methodname">mysqli::multi_query()</a></span>. The individual statements
   of the statement string are separated by semicolon.
   Then, all result sets returned by the executed statements must be fetched.
  </p>
  <p class="para">
   The MySQL server allows having statements that do return result sets and
   statements that do not return result sets in one multiple statement.
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 Multiple Statements</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />mysqli_report</span><span style="color: #007700">(</span><span style="color: #0000BB">MYSQLI_REPORT_ERROR </span><span style="color: #007700">| </span><span style="color: #0000BB">MYSQLI_REPORT_STRICT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">, </span><span style="color: #DD0000">"user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"DROP TABLE IF EXISTS test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE test(id INT)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT COUNT(*) AS _num FROM test;<br />        INSERT INTO test(id) VALUES (1); <br />        SELECT COUNT(*) AS _num FROM test; "</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">multi_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br />do {<br />    if (</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">store_result</span><span style="color: #007700">()) {<br />        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_all</span><span style="color: #007700">(</span><span style="color: #0000BB">MYSQLI_ASSOC</span><span style="color: #007700">));<br />        </span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">free</span><span style="color: #007700">();<br />    }<br />} while (</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">next_result</span><span style="color: #007700">());</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [0]=&gt;
  array(1) {
    [&quot;_num&quot;]=&gt;
    string(1) &quot;0&quot;
  }
}
array(1) {
  [0]=&gt;
  array(1) {
    [&quot;_num&quot;]=&gt;
    string(1) &quot;1&quot;
  }
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <strong>Security considerations</strong>
  </p>
  <p class="para">
   The API functions <span class="methodname"><a href="mysqli.query.php" class="methodname">mysqli::query()</a></span> and
   <span class="methodname"><a href="mysqli.real-query.php" class="methodname">mysqli::real_query()</a></span> do not set a connection flag necessary
   for activating multi queries in the server. An extra API call is used for
   multiple statements to reduce the damage of accidental SQL injection
   attacks. An attacker may try to add statements such as
   <code class="literal">; DROP DATABASE mysql</code> or <code class="literal">; SELECT SLEEP(999)</code>.
   If the attacker succeeds in adding SQL to the statement string but
   <span class="methodname"><a href="mysqli.multi-query.php" class="methodname">mysqli::multi_query()</a></span> is not used, the server will not
   execute the injected and malicious SQL statement.
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 SQL Injection</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />mysqli_report</span><span style="color: #007700">(</span><span style="color: #0000BB">MYSQLI_REPORT_ERROR </span><span style="color: #007700">| </span><span style="color: #0000BB">MYSQLI_REPORT_STRICT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">, </span><span style="color: #DD0000">"user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT 1; DROP TABLE mysql.user"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
PHP Fatal error:  Uncaught mysqli_sql_exception: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to 
use near &#039;DROP TABLE mysql.user&#039; at line 1
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <strong>Prepared statements</strong>
  </p>
  <p class="para">
   Use of the multiple statement with prepared statements is not supported.
  </p>
  <p class="para">
   <strong>See also</strong>
  </p>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="mysqli.query.php" class="methodname">mysqli::query()</a></span></li>
    <li><span class="methodname"><a href="mysqli.multi-query.php" class="methodname">mysqli::multi_query()</a></span></li>
    <li><span class="methodname"><a href="mysqli.next-result.php" class="methodname">mysqli::next_result()</a></span></li>
    <li><span class="methodname"><a href="mysqli.more-results.php" class="methodname">mysqli::more_results()</a></span></li>
   </ul>
  </p>
 </div><?php manual_footer($setup); ?>