<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'mysqli.query.php',
    1 => 'mysqli::query',
    2 => 'Performs a query on the database',
  ),
  'up' => 
  array (
    0 => 'class.mysqli.php',
    1 => 'mysqli',
  ),
  'prev' => 
  array (
    0 => 'mysqli.prepare.php',
    1 => 'mysqli::prepare',
  ),
  'next' => 
  array (
    0 => 'mysqli.real-connect.php',
    1 => 'mysqli::real_connect',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysqli/mysqli/query.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

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

 </div>

 <div class="refsect1 description" id="refsect1-mysqli.query-description">
  <h3 class="title">Description</h3>
  <p class="para">Object-oriented style</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">function</span> <span class="methodname"><strong>mysqli::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="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$result_mode</code><span class="initializer"> = <strong><code><a href="mysqli.constants.php#constant.mysqli-store-result">MYSQLI_STORE_RESULT</a></code></strong></span></span>): <span class="type"><span class="type"><a href="class.mysqli-result.php" class="type mysqli_result">mysqli_result</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></div>

  <p class="para rdfs-comment">Procedural style</p>
  <div class="methodsynopsis dc-description"><span class="modifier">function</span> <span class="methodname"><strong>mysqli_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="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$result_mode</code><span class="initializer"> = <strong><code><a href="mysqli.constants.php#constant.mysqli-store-result">MYSQLI_STORE_RESULT</a></code></strong></span></span>): <span class="type"><span class="type"><a href="class.mysqli-result.php" class="type mysqli_result">mysqli_result</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></div>

  <p class="para rdfs-comment">
   Performs a <code class="parameter">query</code> against the database.
  </p>
  <div class="warning"><strong class="warning">Warning</strong>
<h1 class="title">Security warning: SQL injection</h1><p class="simpara">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">
   For non-DML queries (not INSERT, UPDATE or DELETE),
   this function is similar to calling 
   <span class="function"><a href="mysqli.real-query.php" class="function">mysqli_real_query()</a></span> followed by either
   <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>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    In the case where a statement is passed to
    <span class="function"><strong>mysqli_query()</strong></span> that is longer than
    <code class="literal">max_allowed_packet</code> of the server, the returned
    error codes are different depending on whether you are using MySQL
    Native Driver (<code class="literal">mysqlnd</code>) or MySQL Client Library
    (<code class="literal">libmysqlclient</code>). The behavior is as follows:
   </p>
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      <code class="literal">mysqlnd</code> on Linux returns an error code of 1153.
      The error message means <q class="quote">got a packet bigger than
      <code class="literal">max_allowed_packet</code> bytes</q>.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <code class="literal">mysqlnd</code> on Windows returns an error code 2006.
      This error message means <q class="quote">server has gone away</q>.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <code class="literal">libmysqlclient</code> on all platforms returns an error code
      2006. This error message means <q class="quote">server has gone
      away</q>.
     </p>
    </li>
   </ul>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysqli.query-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    <dt>
<code class="parameter">mysql</code></dt><dd><span class="simpara">Procedural style only: A <span class="classname"><a href="class.mysqli.php" class="classname">mysqli</a></span> object
returned by <span class="function"><a href="function.mysqli-connect.php" class="function">mysqli_connect()</a></span> or <span class="function"><a href="mysqli.init.php" class="function">mysqli_init()</a></span>
</span></dd>
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       The query string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">result_mode</code></dt>
     <dd>
      <p class="para">
       The result mode can be one of 3 constants indicating how the result will
       be returned from the MySQL server.
      </p>
      <p class="para">
       <strong><code><a href="mysqli.constants.php#constant.mysqli-store-result">MYSQLI_STORE_RESULT</a></code></strong> (default) - returns a
       <span class="classname"><a href="class.mysqli-result.php" class="classname">mysqli_result</a></span> object with buffered result set.
      </p>
      <p class="para">
       <strong><code><a href="mysqli.constants.php#constant.mysqli-use-result">MYSQLI_USE_RESULT</a></code></strong> - returns a
       <span class="classname"><a href="class.mysqli-result.php" class="classname">mysqli_result</a></span> object with unbuffered result set. 
       As long as there are pending records waiting to be fetched, the
       connection line will be busy and all subsequent calls will return error 
       <code class="literal">Commands out of sync</code>. To avoid the error all records 
       must be fetched from the server or the result set must be discarded by
       calling <span class="function"><a href="mysqli-result.free.php" class="function">mysqli_free_result()</a></span>.
      </p>
      <p class="para">
       <strong><code><a href="mysqli.constants.php#constant.mysqli-async">MYSQLI_ASYNC</a></code></strong> (available with mysqlnd) - the query is
       performed asynchronously and no result set is immediately returned.
       <span class="function"><a href="mysqli.poll.php" class="function">mysqli_poll()</a></span> is then used to get results from such
       queries. Used in combination with either
       <strong><code><a href="mysqli.constants.php#constant.mysqli-store-result">MYSQLI_STORE_RESULT</a></code></strong> or
       <strong><code><a href="mysqli.constants.php#constant.mysqli-use-result">MYSQLI_USE_RESULT</a></code></strong> constant.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysqli.query-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure. For successful queries which produce a result
   set, such as <code class="literal">SELECT, SHOW, DESCRIBE</code> or
   <code class="literal">EXPLAIN</code>, <span class="function"><strong>mysqli_query()</strong></span> will return
   a <span class="classname"><a href="class.mysqli-result.php" class="classname">mysqli_result</a></span> object. For other successful queries,
   <span class="function"><strong>mysqli_query()</strong></span> will
   return <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mysqli.query-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="simpara">
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.query-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="methodname"><strong>mysqli::query()</strong></span> example</strong></p>
   <div class="example-contents"><p>Object-oriented style</p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

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">);
</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">);

</span><span style="color: #FF8000">/* Create table doesn't return a resultset */
</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 TEMPORARY TABLE myCity LIKE City"</span><span style="color: #007700">);
</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Table myCity successfully created.\n"</span><span style="color: #007700">);

</span><span style="color: #FF8000">/* Select queries return a resultset */
</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 Name FROM City LIMIT 10"</span><span style="color: #007700">);
</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Select returned %d rows.\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">num_rows</span><span style="color: #007700">);

</span><span style="color: #FF8000">/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
</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 * FROM City"</span><span style="color: #007700">, </span><span style="color: #0000BB">MYSQLI_USE_RESULT</span><span style="color: #007700">);

</span><span style="color: #FF8000">/* Note, that we can't execute any functions which interact with the
    server until all records have been fully retrieved or the result
    set was closed. All calls will return an 'out of sync' error */
</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">"SET @a:='this will not work'"</span><span style="color: #007700">);</span></code></pre></div>
   </div>

   <div class="example-contents"><p>Procedural style</p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

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">);
</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">);

</span><span style="color: #FF8000">/* Create table doesn't return a resultset */
</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">"CREATE TEMPORARY TABLE myCity LIKE City"</span><span style="color: #007700">);
</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Table myCity successfully created.\n"</span><span style="color: #007700">);

</span><span style="color: #FF8000">/* Select queries return a resultset */
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT Name FROM City LIMIT 10"</span><span style="color: #007700">);
</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Select returned %d rows.\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">mysqli_num_rows</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">));

</span><span style="color: #FF8000">/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT * FROM City"</span><span style="color: #007700">, </span><span style="color: #0000BB">MYSQLI_USE_RESULT</span><span style="color: #007700">);

</span><span style="color: #FF8000">/* Note, that we can't execute any functions which interact with the
    server until all records have been fully retrieved or the result
    set was closed. All calls will return an 'out of sync' error */
</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">"SET @a:='this will not work'"</span><span style="color: #007700">);</span></code></pre></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">Table myCity successfully created.
Select returned 10 rows.

Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can&#039;t run this command now in...</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mysqli.query-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="mysqli.execute-query.php" class="function" rel="rdfs-seeAlso">mysqli_execute_query()</a> - Prepares, binds parameters, and executes SQL statement</span></li>
    <li><span class="function"><a href="mysqli.real-query.php" class="function" rel="rdfs-seeAlso">mysqli_real_query()</a> - Execute an SQL query</span></li>
    <li><span class="function"><a href="mysqli.multi-query.php" class="function" rel="rdfs-seeAlso">mysqli_multi_query()</a> - Performs one or more queries on the database</span></li>
    <li><span class="function"><a href="mysqli.prepare.php" class="function" rel="rdfs-seeAlso">mysqli_prepare()</a> - Prepares an SQL statement for execution</span></li>
    <li><span class="function"><a href="mysqli-result.free.php" class="function" rel="rdfs-seeAlso">mysqli_free_result()</a> - Frees the memory associated with a result</span></li>
   </ul>
  </p>
 </div>


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