Home >Backend Development >PHP Tutorial >Solution to PHP Fatal error: Call to undefined function mysql_connect()

Solution to PHP Fatal error: Call to undefined function mysql_connect()

WBOY
WBOYOriginal
2023-06-22 22:30:041860browse

In PHP, we often need to connect to the database and operate on the data. However, when connecting to a MySQL database, you sometimes encounter a common error – PHP Fatal error: Call to undefined function mysql_connect(). This error is related to the MySQL extension and may cause the connection to fail. This article will introduce some common solutions.

1. Check whether PHP has the MySQL extension installed

In your PHP installation, the MySQL extension must be installed in order to establish a connection with the MySQL database. On Linux systems, you can use the following command to check whether the MySQL extension is installed:

php -m | grep mysql

If the output is empty, it means that the MySQL extension is not installed. The command to install the MySQL extension in CentOS is as follows:

sudo yum install php-mysql

If you are using another Linux distribution, please use the corresponding package manager to install the MySQL extension.

2. Check whether the PHP.ini file is correctly configured

When connecting to the MySQL database, you need to ensure that the MySQL extension in the php.ini file is correctly configured. In the php.ini file, find the following lines:

;extension=mysql.so

Make sure there are no semicolons on this line. If there is a semicolon, the line is a comment and the MySQL extension is disabled.

If you are using another operating system or a different version of PHP, please consult the PHP manual to learn the correct way to configure the MySQL extension.

3. Check the PHP version

The PHP version you are using may not be compatible with the MySQL extension. You can check the PHP version using the following command:

php -v

If your PHP version is too old (version number is lower than 5.5), you may need to update your PHP version.

If you are unable to update the PHP version, you can try to use MySQLi or PDO extensions to connect to the MySQL database instead of the MySQL extension.

4. Other solutions

If your PHP environment has been configured correctly and the version is normal, but you still cannot connect to the MySQL database, there are some other solutions for you to try. For example:

  • Make sure the MySQL server is running properly, you can check with the following command:

    sudo service mysqld status
  • Make sure you are using the correct MySQL username and password.
  • Check whether the corresponding MySQL database exists.

Conclusion

In this article, we introduced the causes and solutions of PHP Fatal error: Call to undefined function mysql_connect(). If you encounter this error, you can troubleshoot it one by one according to the above methods. If you still can't resolve the issue, check out the PHP manual for more in-depth help.

The above is the detailed content of Solution to PHP Fatal error: Call to undefined function mysql_connect(). 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