PHP mysqli_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_query() runs one SQL statement on the connection and returns a mysqli_result for SELECT-style queries, true for successful non-result statements, or false on failure.

The default result mode stores (buffers) the full result so mysqli_num_rows() works immediately. Use MYSQLI_USE_RESULT for unbuffered streaming, or prepare/bind when the SQL includes user values.

Syntax

Call mysqli_query() with the values you need. Start with this line.

PHP
$result = mysqli_query($mysqli, "SELECT id, name FROM users");
CallWhat it does
mysqli_query($mysqli, "SELECT 1")Returns a mysqli_result for a SELECT.
mysqli_query($mysqli, "UPDATE t SET a = 1")Returns true on success when there is no result set.
mysqli_query($mysqli, $sql, MYSQLI_USE_RESULT)Returns an unbuffered result (advanced).

Only one statement per call — for semicolon batches use mysqli_multi_query(). For values from users, prefer mysqli_prepare() instead of gluing strings. query() is similar to real_query() plus store_result()/use_result() in one step.

Library declarations
PHP
mysqli_query(mysqli $mysql, string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool

Parameters

ParameterDescription
mysqlRequired. The open mysqli connection.
queryRequired. A single SQL statement string.
result_modeOptional. MYSQLI_STORE_RESULT (default), MYSQLI_USE_RESULT, or MYSQLI_ASYNC (mysqlnd).

Return Value

Return ValueDescription
mysqli_result|boolmysqli_result for successful SELECT/SHOW-style queries; true for successful INSERT/UPDATE/DELETE-style queries; false on failure.

PHP Examples

Example 1 — SELECT and Count Rows

Run a SELECT with the default stored mode and print the row count.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$result = mysqli_query($mysqli, "SELECT id, name FROM users LIMIT 3");
if ($result === false) {
    echo mysqli_error($mysqli), PHP_EOL;
    exit;
}

echo "Rows: ", mysqli_num_rows($result), PHP_EOL;
mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. mysqli_query() runs one SELECT.
  2. A mysqli_result is returned on success.
  3. Default store mode lets num_rows report 3.
  4. Free the result and close the connection.

Example 2 — Non-SELECT Returns True

INSERT/UPDATE style statements do not return a result object.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$ok = mysqli_query(
    $mysqli,
    "INSERT INTO logs (msg) VALUES ('hello')"
);

var_dump($ok);
echo "insert_id=", mysqli_insert_id($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Successful INSERT returns true, not a result set.
  2. Use mysqli_insert_id() or mysqli_affected_rows() for details.
  3. false would mean the statement failed.
  4. Do not call mysqli_fetch_assoc() on true.

Example 3 — Prefer Prepare for User Values

Contrast unsafe string building with the prepare mindset.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
$name = "Ada"; // imagine this came from a form

// Better than: "SELECT ... WHERE name = '$name'"
$stmt = mysqli_prepare($mysqli, "SELECT id FROM users WHERE name = ?");
mysqli_stmt_bind_param($stmt, "s", $name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

echo "Matches: ", mysqli_num_rows($result), PHP_EOL;

mysqli_stmt_close($stmt);
mysqli_close($mysqli);

How It Works

  1. User-provided values belong in bound parameters.
  2. prepare + bind replaces unsafe quoting in query strings.
  3. query() remains great for fixed SQL without variables.
  4. The sample finds one matching row.

Frequently Asked Questions

A mysqli_result for SELECT-style success, true for other successful statements, or false on failure.
query() runs one statement. multi_query() can run several semicolon-separated statements and needs a drain loop.
Whenever SQL includes values from users or outside input. Prepared statements avoid injection mistakes.
Unbuffered mode. Rows stream from the server; num_rows is unreliable until fetched, and the connection stays busy until finished.
query() is convenience: it runs the SQL and stores or uses the result for you. real_query() only sends SQL; you call store_result()/use_result() yourself.

Next: mysqli_quote_string()

mysqli_quote_string() (PHP 8.6+) escapes a string using the connection charset and wraps i.

mysqli_quote_string() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful