PHP mysqli_real_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_real_query() sends one SQL statement to MySQL and returns true if the server accepted it, without automatically returning a result object.

For SELECT-style statements you then call mysqli_store_result() or mysqli_use_result() yourself. mysqli_query() is the convenience wrapper that runs real_query and then store/use result in one step — beginners usually want query().

Syntax

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

PHP
$ok = mysqli_real_query($mysqli, $sql);
CallWhat it does
mysqli_real_query($mysqli, "SELECT id FROM users")Sends SQL; returns true/false only.
mysqli_real_query($mysqli, $sql) && ($r = mysqli_store_result($mysqli))Sends SQL then buffers the result.
mysqli_real_query($mysqli, $sql) && ($r = mysqli_use_result($mysqli))Sends SQL then opens an unbuffered result.

Use real_query when you want to choose store_result vs use_result explicitly, or when you only care that a non-SELECT statement started. For async work, query(..., MYSQLI_ASYNC) is the related advanced path, not everyday real_query.

Library declarations
PHP
mysqli_real_query(mysqli $mysql, string $query): bool

Parameters

ParameterDescription
mysqlRequired. The open mysqli connection.
queryRequired. A single SQL statement.

Return Value

Return ValueDescription
booltrue if the statement was accepted; false on failure.

PHP Examples

Example 1 — Real Query Then Store Result

Split send + fetch for a SELECT, with fixed sample output.

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

if (!mysqli_real_query($mysqli, "SELECT name FROM users LIMIT 1")) {
    echo mysqli_error($mysqli), PHP_EOL;
    exit;
}

$result = mysqli_store_result($mysqli);
$row = mysqli_fetch_assoc($result);
echo $row["name"], PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. real_query sends the SELECT.
  2. store_result pulls a buffered mysqli_result.
  3. fetch_assoc reads the sample name.
  4. Free the result afterward.

Example 2 — Non-SELECT Needs No store_result

For INSERT/UPDATE, true is enough — there is no result set.

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

$ok = mysqli_real_query(
    $mysqli,
    "UPDATE users SET active = 1 WHERE id = 1"
);

echo $ok ? "OK" : "Fail", PHP_EOL;
echo "affected=", mysqli_affected_rows($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. real_query runs the UPDATE.
  2. No store_result call is needed.
  3. affected_rows reports how many rows changed.
  4. Calling store_result anyway would simply find no result set.

Example 3 — Same Job With mysqli_query()

Show why beginners usually call query() instead.

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

$result = mysqli_query($mysqli, "SELECT name FROM users LIMIT 1");
$row = mysqli_fetch_assoc($result);
echo $row["name"], PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. query() sends SQL and stores the result for you.
  2. The fetch step looks the same afterward.
  3. Use real_query when you intentionally choose use_result vs store_result.
  4. Otherwise prefer the shorter query() form.

Frequently Asked Questions

Only true or false. It does not return a mysqli_result object.
Call mysqli_store_result() or mysqli_use_result() on the same connection.
query() combines real_query with store/use result, which is simpler for most code.
No. real_query runs one statement. multi_query can run several and needs more_results/next_result.
For large result streams when you want unbuffered fetching. Beginners should prefer store_result / default query().

Next: mysqli_reap_async_query()

mysqli_reap_async_query() collects the result of a query that was started asynchronously o.

mysqli_reap_async_query() 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