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-04 02:22:13
Original
365 people have browsed it

Why Does mysqli_real_escape_string() Throw a

Understanding the mysqli_real_escape_string Error Message

When attempting to use mysqli_real_escape_string(), developers may encounter the error "mysqli_real_escape_string() expects exactly 2 parameters, 1 given." This error stems from misunderstandings surrounding the function's parameter requirements.

Function Definition

mysqli_real_escape_string() requires two parameters:

  1. $link: A link identifier for an open MySQL connection.
  2. $escapestr: The string to be escaped.

Troubleshooting the Error

To resolve the error, ensure you provide both parameters correctly. Here's a corrected code snippet:

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

In this code, $link represents the MySQL connection link, and $string is the string to be escaped.

The error commonly occurs when you fail to provide the connection link. Remember, mysqli_real_escape_string() operates on an established database connection.

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