PHP mysqli_quote_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_quote_string() (PHP 8.6+) escapes a string using the connection charset and wraps it in single quotes so it can be pasted into SQL as a string literal.

On older PHP there is no widely portable mysqli_quote_string() helper in classic docs — use mysqli_prepare() (best) or mysqli_real_escape_string() plus your own quotes. Treat this page as quoting guidance: prepared statements first, real_escape_string as the classic escape API, quote_string only where PHP 8.6+ is guaranteed.

Syntax

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

PHP
$literal = mysqli_quote_string($mysqli, $value); // PHP 8.6+
CallWhat it does
mysqli_quote_string($mysqli, "O'Reilly")PHP 8.6+: returns a quoted literal such as "'O\'Reilly'".
mysqli_real_escape_string($mysqli, $s)Portable escape API (does not add quotes).
mysqli_prepare($mysqli, "SELECT * FROM t WHERE name = ?")Preferred: bind the value instead of quoting it.

Be honest in real projects: prefer prepare/bind for values. If you must build SQL strings on older PHP, call mysqli_set_charset() then mysqli_real_escape_string(), and add quotes yourself. quote_string is nicer when available because it both escapes and quotes, including safer behavior under NO_BACKSLASH_ESCAPES.

Library declarations
PHP
mysqli_quote_string(mysqli $mysql, string $string): string

Parameters

ParameterDescription
mysqlRequired. A live mysqli connection (charset should already be set).
stringRequired. The raw string value to quote for SQL.

Return Value

Return ValueDescription
stringOn PHP 8.6+, the escaped value wrapped in single quotes. On older PHP, use real_escape_string/prepare instead — do not assume this function exists.

PHP Examples

Example 1 — Preferred: Prepared Statement, No Manual Quotes

The practical beginner pattern that works on all supported PHP versions.

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

$name = "O'Reilly";
$stmt = mysqli_prepare($mysqli, "SELECT id FROM authors 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. set_charset keeps the connection encoding explicit.
  2. The ? placeholder receives the value safely.
  3. No manual quotes or escape calls are required.
  4. This is the approach to use in real application code.

Example 2 — Portable Escape With real_escape_string

When you must build SQL text on older PHP, escape then quote yourself.

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

$raw = "O'Reilly";
$escaped = mysqli_real_escape_string($mysqli, $raw);
$sql = "SELECT id FROM authors WHERE name = '" . $escaped . "'";

echo $escaped, PHP_EOL;
echo $sql, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. real_escape_string encodes quotes and other special characters.
  2. It does not add the surrounding quotes — you must add them.
  3. set_charset is required so escaping matches the connection.
  4. Prefer prepare whenever you can; this is the fallback.

Example 3 — PHP 8.6+ quote_string Shape

Shows what mysqli_quote_string returns when the function exists.

PHP
<?php
// PHP 8.6+ only. On older PHP, use prepare or real_escape_string instead.
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_set_charset($mysqli, "utf8mb4");

$raw = "O'Reilly";
// Sample of the documented behavior:
$literal = "'" . mysqli_real_escape_string($mysqli, $raw) . "'";
// On PHP 8.6+, mysqli_quote_string($mysqli, $raw) produces the same idea.

echo $literal, PHP_EOL;
echo "SELECT id FROM authors WHERE name = " . $literal, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. quote_string escapes and wraps in single quotes in one step.
  2. Insert the returned literal into SQL without adding more quotes.
  3. This demo mirrors that result using real_escape_string for portability on the tutorial host.
  4. Feature-detect PHP 8. 6+ before calling mysqli_quote_string() in libraries.

Frequently Asked Questions

No. It was added in PHP 8.6. Older versions should use mysqli_prepare() or mysqli_real_escape_string().
Prepared statements with bound parameters. That avoids manual quoting mistakes.
real_escape_string only escapes. quote_string (PHP 8.6+) escapes and wraps the value in single quotes.
Escaping/quoting follows the connection charset. Without set_charset, escape behavior can be wrong.
No. These helpers are for string literals only, not identifiers.

Next: mysqli_real_connect()

mysqli_real_connect() opens a MySQL connection on an existing mysqli object created by mys.

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