<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.mysqli.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'mysqli.multi-query.php',
    1 => 'mysqli::multi_query',
    2 => 'Performs one or more queries on the database',
  ),
  'up' => 
  array (
    0 => 'class.mysqli.php',
    1 => 'mysqli',
  ),
  'prev' => 
  array (
    0 => 'mysqli.more-results.php',
    1 => 'mysqli::more_results',
  ),
  'next' => 
  array (
    0 => 'mysqli.next-result.php',
    1 => 'mysqli::next_result',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysqli/mysqli/multi-query.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mysqli.multi-query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysqli::multi_query</h1>
  <h1 class="refname">mysqli_multi_query</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mysqli::multi_query</span> -- <span class="refname">mysqli_multi_query</span> &mdash; <span class="dc-title">Performs one or more queries on the database</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mysqli.multi-query-description">
  <h3 class="title">Descrizione</h3>
  <p class="para">Stile orientato agli oggetti</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>mysqli::multi_query</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">Stile procedurale</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>mysqli_multi_query</strong></span>(<span class="methodparam"><span class="type"><a href="class.mysqli.php" class="type mysqli">mysqli</a></span> <code class="parameter">$mysql</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Executes one or multiple queries which are concatenated by a semicolon.
  </p>
  <div class="warning"><strong class="warning">Avviso</strong>
<h1 class="title">Security warning: SQL injection</h1><p class="para">If the query contains any variable
input then <a href="mysqli.quickstart.prepared-statements.php" class="link">parameterized
prepared statements</a> should be used instead. Alternatively, the
data must be properly formatted and all strings must be escaped using
the <span class="function"><a href="mysqli.real-escape-string.php" class="function">mysqli_real_escape_string()</a></span>
function.</p></div>
  <p class="para">
   Queries are sent in a single call to the database and processed sequentially.
   <span class="methodname"><strong>mysqli_multi_query()</strong></span> waits for the first query to
   complete before returning control to PHP. In the meantime, the MySQL server
   will continue processing the remaining queries asynchronously of PHP and make
   the results available for fetching.
  </p>
  <p class="para">
   It is recommended to use
   <a href="control-structures.do.while.php" class="link">do-while</a> to process multiple
   queries. The connection will be busy until all queries have completed and
   their results are fetched to PHP. No other statement can be issued on the
   same connection until all queries are processed.
   To proceed to the next query in the sequence, use
   <span class="function"><a href="mysqli.next-result.php" class="function">mysqli_next_result()</a></span>. If the next result is not ready yet,
   mysqli will wait for the response from the MySQL server.
   To check if there are more results,
   use <span class="function"><a href="mysqli.more-results.php" class="function">mysqli_more_results()</a></span>.
  </p>
  <p class="para">
   For queries which produce a result set, such as
   <code class="literal">SELECT, SHOW, DESCRIBE</code> or
   <code class="literal">EXPLAIN</code>,
   <span class="function"><a href="mysqli.use-result.php" class="function">mysqli_use_result()</a></span> or <span class="function"><a href="mysqli.store-result.php" class="function">mysqli_store_result()</a></span>
   can be used to retrieve the result set. For queries which do not produce a
   result set, the same functions can be used to retrieve information such as
   the number of affected rows.
  </p>
  <div class="tip"><strong class="tip">Suggerimento</strong>
   <p class="para">
    Executing <code class="literal">CALL</code> statements for stored procedures can
    produce multiple result sets. If the stored procedure contains
    <code class="literal">SELECT</code> statements, the result sets are returned in the
    order that they are produced as the procedure executes. In general, the
    caller cannot know how many result sets a procedure will return and must be
    prepared to retrieve multiple results. The final result from the procedure
    is a status result that includes no result set. The status indicates whether
    the procedure succeeded or an error occurred.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysqli.multi-query-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    <dt><code class="parameter">
link</code></dt><dd><p class="para">Solo nello stile procedurale: un identificatore
restituito da <span class="function"><a href="function.mysqli-connect.php" class="function">mysqli_connect()</a></span> o <span class="function"><a href="mysqli.init.php" class="function">mysqli_init()</a></span>
</p></dd>
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       A string containing the queries to be executed.
       Multiple queries must be separated by a semicolon.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysqli.multi-query-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if the first statement failed.
   To retrieve subsequent errors from other statements you have to call
   <span class="function"><a href="mysqli.next-result.php" class="function">mysqli_next_result()</a></span> first.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mysqli.multi-query-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <p class="para">
If mysqli error reporting is enabled (<strong><code><a href="mysqli.constants.php#constant.mysqli-report-error">MYSQLI_REPORT_ERROR</a></code></strong>) and the requested operation fails,
a warning is generated. If, in addition, the mode is set to <strong><code><a href="mysqli.constants.php#constant.mysqli-report-strict">MYSQLI_REPORT_STRICT</a></code></strong>,
a <span class="classname"><a href="class.mysqli-sql-exception.php" class="classname">mysqli_sql_exception</a></span> is thrown instead.</p>
 </div>


 <div class="refsect1 examples" id="refsect1-mysqli.multi-query-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="methodname"><strong>mysqli::multi_query()</strong></span> example</strong></p>
   <div class="example-contents"><p>Stile orientato agli oggetti</p></div>
   <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">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT CURRENT_USER();"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">.= </span><span style="color: #DD0000">"SELECT Name FROM City ORDER BY ID LIMIT 20, 5"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* execute multi query */<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">$query</span><span style="color: #007700">);<br />do {<br />    </span><span style="color: #FF8000">/* store the result set in PHP */<br />    </span><span style="color: #007700">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 />        while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">()) {<br />            </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />        }<br />    }<br />    </span><span style="color: #FF8000">/* print divider */<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">more_results</span><span style="color: #007700">()) {<br />        </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----------------\n"</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>Stile procedurale</p></div>
   <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">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT CURRENT_USER();"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">.= </span><span style="color: #DD0000">"SELECT Name FROM City ORDER BY ID LIMIT 20, 5"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* execute multi query */<br /></span><span style="color: #0000BB">mysqli_multi_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />do {<br />    </span><span style="color: #FF8000">/* store the result set in PHP */<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_store_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">)) {<br />        while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br />            </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />        }<br />    }<br />    </span><span style="color: #FF8000">/* print divider */<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">mysqli_more_results</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">)) {<br />        </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----------------\n"</span><span style="color: #007700">);<br />    }<br />} while (</span><span style="color: #0000BB">mysqli_next_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">));</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above examples will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mysqli.multi-query-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="mysqli.query.php" class="function" rel="rdfs-seeAlso">mysqli_query()</a> - Performs a query on the database</span></li>
    <li><span class="function"><a href="mysqli.use-result.php" class="function" rel="rdfs-seeAlso">mysqli_use_result()</a> - Initiate a result set retrieval</span></li>
    <li><span class="function"><a href="mysqli.store-result.php" class="function" rel="rdfs-seeAlso">mysqli_store_result()</a> - Transfers a result set from the last query</span></li>
    <li><span class="function"><a href="mysqli.next-result.php" class="function" rel="rdfs-seeAlso">mysqli_next_result()</a> - Prepare next result from multi_query</span></li>
    <li><span class="function"><a href="mysqli.more-results.php" class="function" rel="rdfs-seeAlso">mysqli_more_results()</a> - Check if there are any more query results from a multi query</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>