Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given

This error message indicates a fundamental mismatch in how you are managing your database connection object versus how you intend to use it. The function mysqli_real_escape_string() is designed specifically for the MySQLi extension to handle SQL injection safely. It requires two arguments: the raw string you want to sanitize, and the active connection object. Your code passes $con as the second argument, which is correct, but PHP throws this specific error because it interprets your first argument, trim($_POST["name"]), incorrectly in the context of older PHP versions or specific configurations.

However, there is a more critical architectural issue at play here that goes beyond simple syntax errors: mixing procedural and object-oriented styles without proper abstraction, and relying on extract() to populate variables from an array. The line $my_array = array($c_z); followed by extract($my_array); attempts to force the variable $c_z into a single-element array and then extract it into the global scope. This is unnecessary and fragile. If $c_z is empty, you are extracting an empty string, which creates a variable named $c_z with no value. More importantly, this pattern suggests you might be trying to fetch columns directly from SELECT * without mapping them explicitly, which often leads to errors when column names change or order shifts.

To fix the immediate error and the underlying logic flaw, you need to ensure that the variable passed to mysqli_real_escape_string() is a string, and that your connection object is valid. The error "expects parameter 1 to be mysqli" is slightly confusing because it usually means the first argument isn't what you think it is, but in this specific case, it often stems from running code on a PHP version where the function signature handling differs or if the variable passed was inadvertently interpreted as an object instead of a string in a legacy context.

The most robust solution involves separating data retrieval from data submission and strictly using prepared statements. While mysqli_real_escape_string is part of the procedural API, modern development practices strongly favor Prepared Statements (mysqli_prepare, mysqli_stmt_bind_param). They handle escaping automatically and are immune to SQL injection attacks regardless of user input.

Here is how you should restructure your updated.php file. First, fetch the data using a prepared statement to avoid SQL injection risks during the read phase:

<?php
// Create connection
$con = mysqli_connect("xxx", "xxx", "xxx", "xxx");

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id']; // This is where you load the page initially

// Fetch existing data safely
$stmt = $con->prepare("SELECT name FROM characters WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

$name_current = $row['name'] ?? ''; // Default to empty string if not found

// Now handle the form submission logic
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id_update = $_POST['id'];
    $name_input = trim($_POST["name"]);

    // Use prepared statements for updates as well
    $stmtUpdate = $con->prepare("UPDATE characters SET name=? WHERE id=?");
    $stmtUpdate->bind_param("si", $name_input, $id_update);

    if ($stmtUpdate->execute()) {
        echo "Successfully updated";
    } else {
        die('Invalid query: ' . $stmtUpdate->error);
    }
}
?>

For the HTML form, you must ensure that the input value reflects the data fetched from the database. If no $_POST exists (because it's a GET request initially), use the $name_current variable retrieved earlier:

<input name="name" maxlength="255" type="text" value="<?php echo htmlspecialchars($name_current); ?>">

Using htmlspecialchars() here is crucial for preventing Cross-Site Scripting (XSS) when rendering user-controlled data into HTML attributes. Avoid using stripslashes() in your output logic; PHP handles escape sequences automatically, and manually stripping them can reintroduce security vulnerabilities.

Finally, consider transitioning away from the procedural MySQLi API entirely if you are building new features. The object-oriented interface offers better namespace management and cleaner code structure. If you are maintaining legacy code, ensure that every instance of mysqli_connect is followed by a strict check for errors before attempting any queries.

Our network