Example 1 — Escape a Quote Character
Show the escaped form of a name that contains an apostrophe.
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_set_charset($mysqli, "utf8mb4");
$raw = "O'Reilly";
echo mysqli_real_escape_string($mysqli, $raw), PHP_EOL;
mysqli_close($mysqli); How It Works
- Connect and set utf8mb4 explicitly.
- real_escape_string encodes the apostrophe.
- The returned text still needs quotes when placed in SQL.
- Close the connection when done.

