PHP is a widely used open source scripting language for web development. However, developers often face various errors and problems when using PHP for web development. Among them, a common problem is "PHP Fatal error: Call to undefined function mysql_real_escape_string()".
In this article, we will discuss the causes and solutions of this error.
Reason:
The MySQLi extension is a more powerful database extension that replaced the MySQL extension in PHP after version 5.5. If you use the mysql_real_escape_string() function when using the MySQLi extension, the following error will occur:
PHP Fatal error: Call to undefined function mysql_real_escape_string()
This is because of the mysql_real_escape_string() function Is a function of the MySQL extension and is not included in the MySQLi extension.
Solution:
1. Replace the function
To solve this problem, you first need to replace the mysql_real_escape_string() function with the mysqli_real_escape_string() function, because it is a MySQLi extension a function of. For example:
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
$name = mysqli_real_escape_string($mysqli, $_POST['name ']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
This method is relatively simple, but it requires modifying a lot of code and is not suitable for already implemented s project.
2. Use MySQL compatibility mode
If your project uses MySQL compatibility mode, you can enable MySQL compatibility mode by adding a parameter when connecting to the database. For example:
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name', NULL, '/path/to/mysql/socket', MYSQLI_CLIENT_COMPRESS | MYSQL_CLIENT_IGNORE_SPACE);
This method will allow new versions of PHP to run in MySQL-compatible mode, but may cause other problems.
3. Install the MySQL extension
If your project needs to use the mysql_real_escape_string() function, the best way is to install the MySQL extension. To do this, you need to manipulate the operating system to install the older MySQL extension.
For Ubuntu and Debian systems, you can install the extension by running the following command:
sudo apt-get install php5-mysql
For RHEL and CentOS systems, you can install it via Run the following command to install the extension:
sudo yum install php-mysql
4. Upgrade the project
When upgrading the project, consider replacing the mysql_real_escape_string() function with PDO Safe alternatives provided. For example:
$dbh = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$name = $dbh->quote ($_POST['name']);
$email = $dbh->quote($_POST['email']);
The above method can provide high security guarantee , but it requires a full understanding of PDO, which requires additional time and effort.
Summary:
PHP Fatal error: Call to undefined function mysql_real_escape_string() is a common error, but there are many solutions. Before making a fix, you should consider the database extensions used in your project and test all solutions to ensure they have no obvious side effects.
The above is the detailed content of Solution to PHP Fatal error: Call to undefined function mysql_real_escape_string(). For more information, please follow other related articles on the PHP Chinese website!