Home > Backend Development > PHP Tutorial > Why Does `mysqli_real_escape_string()` Throw a 'expects exactly 2 parameters, 1 given' Error?

Why Does `mysqli_real_escape_string()` Throw a 'expects exactly 2 parameters, 1 given' Error?

Patricia Arquette
Release: 2024-12-25 03:57:16
Original
735 people have browsed it

Why Does `mysqli_real_escape_string()` Throw a

Handling "mysqli_real_escape_string() expects exactly 2 parameters, 1 given" Error

In PHP, the mysqli_real_escape_string() function is essential for preventing SQL injection vulnerabilities. However, developers often encounter the error "mysqli_real_escape_string() expects exactly 2 parameters, 1 given."

To understand this error, let's examine the function's declaration:

string mysqli_real_escape_string(mysqli $link, string $escapestr)
Copy after login

It requires two parameters:

  1. $link: A link to a MySQLi connection
  2. $escapestr: The string to be escaped

In the code snippet you provided, you are missing the $link parameter:

if (phpversion() >= '4.3.0') {
    $string = mysqli_real_escape_string($string);
} else {
    $string = mysqli_escape_string($string);
}
Copy after login

To resolve the error, you need to provide the correct number of parameters. For example:

if (phpversion() >= '4.3.0') {
    $string = mysqli_real_escape_string($mysqli, $string);
} else {
    $string = mysqli_escape_string($mysqli, $string);
}
Copy after login

Here, $mysqli represents a valid MySQLi connection link.

The above is the detailed content of Why Does `mysqli_real_escape_string()` Throw a 'expects exactly 2 parameters, 1 given' Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template