<?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 => 'uk',
  ),
  '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">Опис</h3>
  <p class="para">Об&#039;єктно-орієнтований стиль</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">Процедурний стиль</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">Увага</strong><h1 class="title">Попередження з безпеки:
SQL-ін&#039;єкція</h1><p class="para">Якщо рядок запиту має містити введені
користувачем значення, то потрібно виконати <a href="mysqli.quickstart.prepared-statements.php" class="link">підготовлений запит</a>. Або
ж дані мають бути правильно відформатовані, а всі рядки — екрановані функцією
<span class="function"><a href="mysqli.real-escape-string.php" class="function">mysqli_real_escape_string()</a></span>.</p></div>
  <p class="para">
   Queries are sent asynchronously in a single call to the database, but the
   database processes them sequentially.
   <span class="methodname"><strong>mysqli_multi_query()</strong></span> waits for the first query to
   complete before returning control to PHP. The MySQL server will then process
   the next query in the sequence. Once the next result is ready, MySQL will wait
   for the next execution of <span class="function"><a href="mysqli.next-result.php" class="function">mysqli_next_result()</a></span> from PHP.
  </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">Підказка</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">Параметри</h3>
  <p class="para">
   <dl>
    <dt>
<code class="parameter">mysql</code></dt><dd><p class="para">Тільки процедурний стиль:
об&#039;єкт <span class="classname"><a href="class.mysqli.php" class="classname">mysqli</a></span>, якого повертає функція
<span class="function"><a href="function.mysqli-connect.php" class="function">mysqli_connect()</a></span> або
<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">Значення, що повертаються</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">Помилки/виключення</h3>
  <p class="para"> Якщо увімкнені звіти про помилки
mysqli (<strong><code><a href="mysqli.constants.php#constant.mysqli-report-error">MYSQLI_REPORT_ERROR</a></code></strong>) і запитана операція не
виконалась, буде виведено попередження, але якщо встановлено режим
<strong><code><a href="mysqli.constants.php#constant.mysqli-report-strict">MYSQLI_REPORT_STRICT</a></code></strong>, буде викинуто виключення
<span class="classname"><a href="class.mysqli-sql-exception.php" class="classname">mysqli_sql_exception</a></span>.</p>
 </div>


 <div class="refsect1 examples" id="refsect1-mysqli.multi-query-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 <span class="methodname"><strong>mysqli::multi_query()</strong></span> example</strong></p>
   <div class="example-contents"><p>Об&#039;єктно-орієнтований стиль</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>Процедурний стиль</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>Подані вище приклади виведуть
щось схоже на:</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">Прогляньте також</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); ?>