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

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

Linda Hamilton
Linda HamiltonOriginal
2024-12-04 09:51:14645browse

Why Does `mysqli_real_escape_string()` Throw an

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

Question:

When attempting to use the mysqli_real_escape_string() function, the following error is encountered:

mysqli_real_escape_string() expects exactly 2 parameters, 1 given

Answer:

According to the function's documentation, it requires two parameters:

  1. $link: A link to a MySQL connection established using mysqli_connect().
  2. $escapestr: The string to be escaped.

The error indicates that the function is being called with only one parameter, which is likely the string to be escaped.

Example:

The following code snippet provides a correct example of how to use the mysqli_real_escape_string() function:

$mysqli = mysqli_connect('localhost', 'username', 'password', 'database');
$escapedString = mysqli_real_escape_string($mysqli, $string);

In this example, $mysqli represents the connection to the MySQL database, and $string is the string to be escaped. The mysqli_real_escape_string() function will correctly apply escaping to the input string and return the escaped version in $escapedString.

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

Statement:
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