Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given... what I do wrong?

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given... what I do wrong?

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given... what I do wrong?

When you see the error Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given, the issue is almost never a PHP configuration problem or a server environment setting. It is strictly a syntax error in your script. The function mysqli_real_escape_string() requires two arguments: the string you want to escape and the database connection handle. In your code, you are passing only the raw user input (like $_POST['username']) as the first argument, leaving the second parameter—$connect—completely missing.

Looking at your register.php file, the function is called like this:

$username = mysqli_real_escape_string($_POST['username']);

To fix this, you must pass the active database connection object as the second argument. However, before diving into fixing that specific line, we need to address a more significant architectural issue in your code: you are connecting to the database inside the request handler.

In PHP, specifically when using mysqli, you should establish a persistent or reusable connection once and pass it through your logic flow. In your snippet, $connect is defined after you attempt to use mysqli_real_escape_string(). Even if you fix the parameter count, this approach will fail because the connection doesn't exist yet when the escaping functions are called.

Here is the corrected order of operations:

  1. Establish the connection immediately at the top of the block.
  2. Select the database.
  3. Pass that specific connection handle to mysqli_real_escape_string().
  4. Use the prepared statement interface for inserts, rather than string concatenation.

Your current code also relies on the deprecated mysql_* functions (mysql_connect, mysql_select_db, mysql_query). These extensions have been removed in PHP 7.0 and are not available in modern environments like 000webhost's latest stacks. You must migrate to mysqli.

Here is how the logic should look with proper escaping and connection handling:

<?php
if(isset($_POST['submit'])) {
    // 1. Establish connection first
    $connect = new mysqli("mysql.example.com", "username", "password");
    
    // Check connection status immediately
    if ($connect->connect_error) {
        die("Connection failed: " . $connect->connect_error);
    }

    // Select database using the mysqli object method
    $connect->select_db("my_database");

    // 2. Now you can safely use mysqli_real_escape_string with the connection handle
    // Note: While escaping is possible, prepared statements are preferred (see below).
    $username = mysqli_real_escape_string($connect, $_POST['username']);
    $pass = mysqli_real_escape_string($connect, $_POST['pass']);
    $pass1 = mysqli_real_escape_string($connect, $_POST['pass1']);
    $email = mysqli_real_escape_string($connect, $_POST['email']);

    if($username && $pass && $pass1 && $email) {
        if($pass == $pass1) {
            // Using a prepared statement is the gold standard for security
            // It prevents SQL injection better than manual escaping
            $stmt = $connect->prepare("INSERT INTO users VALUES(?, ?, ?)");
            
            // You need to bind parameters. Assuming 's' for string:
            $stmt->bind_param("sss", $username, $pass, $email);
            $stmt->execute();

            echo "You have been registered.";
        } else {
            echo "Password must match.";
        }
    } else {
        echo "All fields are required.";
    }
    
    // Close connection when done
    $connect->close();
}
?>

Why Prepared Statements Matter

You might wonder why I switched from mysqli_real_escape_string() to prepared statements in the example above. Both methods prevent SQL injection, but they solve different problems and have different trade-offs.

mysqli_real_escape_string() escapes special characters (like quotes) so that they are treated as literal text rather than code. While this works for simple inputs like usernames or emails, it becomes fragile when dealing with complex queries involving multiple values. If you construct a query dynamically based on user input, there is always a risk of logic errors in how the string is assembled.

Prepared statements separate the SQL structure from the data entirely. When you call $stmt->prepare("INSERT INTO users VALUES(?, ?, ?)"), the database parses the structure first. The ? placeholders are replaced by the bound variables ($username, $pass, $email) at execution time. This ensures that even if a user inputs a string containing a quote character, the database interprets it strictly as data, not as part of the SQL command.

Handling Passwords Correctly

Your current code stores passwords in plain text: INSERT INTO users VALUES(...). This is a critical security vulnerability. If your database were compromised, every user's password would be visible to an attacker.

You must hash passwords before storing them. The industry standard is the bcrypt algorithm, available via PHP's built-in password_hash() and password_verify() functions. You should never store the raw output of $_POST['pass'] in your database. Instead:

// Hash the password before inserting
$hashedPassword = password_hash($pass, PASSWORD_DEFAULT);

// Update the INSERT query to use the hashed value
$stmt = $connect->prepare("INSERT INTO users (username, password, email) VALUES(?, ?, ?)");
$stmt->bind_param("sss", $username, $hashedPassword, $email);

Summary of Fixes

To resolve your immediate error and secure your application:

  1. Initialize $connect before calling any escaping functions.
  2. Pass the $connect object as the second argument to mysqli_real_escape_string().
  3. Migrate from deprecated mysql_* functions to mysqli.
  4. Adopt prepared statements for all database queries.
  5. Hash passwords using password_hash().

If you are looking to streamline this entire process and reduce the boilerplate code required for authentication, session management, and database abstraction, consider exploring modern PHP frameworks. Tools like Laravel provide robust security features out of the box and handle these low-level details automatically, which is why developers often turn to solutions like https://laravelcompany.com when transitioning from basic scripts to production-ready applications.

Our network