PHP mysqli_execute_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_execute_query() prepares, binds, executes and (for SELECT) returns a result in one step; it is available in PHP 8.2 and newer.

Object-oriented style uses $mysqli->execute_query($query, $params). Pass a SQL string with ? placeholders and an optional list of parameters. It is a convenience helper around prepare + bind + execute + get_result.

Syntax

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

PHP
$result = mysqli_execute_query($mysqli, "SELECT id, name FROM products WHERE id = ?", [10]);
CallWhat it does
mysqli_execute_query($mysqli, "SELECT name FROM products WHERE id = ?", [10])Runs a parameterized SELECT and returns a result.
$mysqli->execute_query("UPDATE products SET stock = ? WHERE id = ?", [5, 10])OO form for a write query; returns true/false.
mysqli_execute_query($mysqli, "SELECT 1")Runs SQL with no parameters.

Requires PHP 8.2+. For older PHP, use mysqli_prepare() and bind_param() yourself. On success, SELECT-style statements return mysqli_result; other statements return true.

Library declarations
PHP
mysqli_execute_query(mysqli $mysql, string $query, ?array $params = null): mysqli_result|bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection.
queryRequired. SQL with optional ? placeholders.
paramsOptional. List of values to bind, or null when there are no placeholders.

Return Value

Return ValueDescription
mysqli_result|boolA result object for result-producing queries, true for successful non-result queries, or false on failure.

PHP Examples

Example 1 — Parameterized SELECT in One Call

Fetch one product by id. Sample output when connected to a demo database (PHP 8.2+).

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_set_charset($mysqli, "utf8mb4");

$result = mysqli_execute_query(
    $mysqli,
    "SELECT name FROM products WHERE id = ?",
    [10]
);
$row = mysqli_fetch_assoc($result);
echo $row["name"], PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. execute_query prepares and runs the SQL with one bound id.
  2. A mysqli_result is returned for the SELECT.
  3. fetch_assoc() reads the demo row.
  4. Sample output prints Notebook.

Example 2 — Object Style Update

Update stock with bound parameters and check success.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->set_charset("utf8mb4");

$ok = $mysqli->execute_query(
    "UPDATE products SET stock = ? WHERE id = ?",
    [3, 10]
);
echo $ok ? "updated" : "failed", PHP_EOL;
echo "affected=", $mysqli->affected_rows, PHP_EOL;

$mysqli->close();

How It Works

  1. OO execute_query binds stock and id.
  2. A non-SELECT returns true on success.
  3. affected_rows reports how many rows changed.
  4. Sample output shows one updated row.

Example 3 — Multiple Placeholders

Filter with two bound values without string concatenation.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_set_charset($mysqli, "utf8mb4");

$result = mysqli_execute_query(
    $mysqli,
    "SELECT name FROM products WHERE category = ? AND price <= ? ORDER BY name",
    ["books", 12.5]
);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row["name"], PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. Two placeholders receive category and price.
  2. Values stay out of the SQL string text.
  3. The while loop prints each matching name.
  4. Sample output when connected to a demo database lists two products.

Frequently Asked Questions

In PHP 8.2+ it prepares a statement, binds optional parameters, executes it, and returns a result for SELECT-style SQL (or true for other successful statements).
No. It was added in PHP 8.2. On older versions use mysqli_prepare(), bind_param() and execute() instead.
Call $mysqli->execute_query($query, $params). Behavior matches the procedural function.
Parameters are sent separately from the SQL template, so user values cannot change the statement structure the way string concatenation can.
true on success (and false on failure). Use affected_rows or insert_id afterward when you need those details.

Next: mysqli_fetch_all()

mysqli_fetch_all() returns every remaining row from a mysqli_result as a nested array in o.

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