PHP Won't Connect to MySQL
One of the features of PHP that makes it popular for designing website content management systems, or CMS, is its ability to connect with the open source MySQL database server software. Considering how common it is for PHP to pair with MySQL when building a CMS, errors in establishing simple connections to a MySQL database might come as a surprise. However, there are a number of simple explanations for resolving this issue.
-
PHP MySQL Compilation Options
-
Depending on what version of PHP your server is using, it may or may not have automatically compiled with the drivers necessary to establish connections to MySQL database servers. Consult the PHP installation manual page to see if your version of PHP came with these drivers enabled by default. If it did not, and you did not compile PHP with the option "--with-mysql[=DIR]" with "DIR" being the directory in which you installed MySQL, then you will have to recompile PHP with this option enabled.
Server Running?
-
PHP and MySQL are two separate software components. Just because your HTML proxy is running the scripts necessary to access the PHP interpreter does not mean that the MySQL database server is up and running. If this server is not running, then PHP will not be able to establish connections to it. You can use the terminal command "top" on UNIX based systems or "tasklist" for Windows to produce a list of processes that are currently running. If MySQL is not among these processes, then you will have to launch it before PHP can establish MySQL connections.
-
Credentials
-
Just as you cannot log in to your computer without your correct user name and password, MySQL will not let PHP establish connections to it if the user name and password you provided as parameters in the "mysql_connect()" function are incorrect. You can double check your source code to make sure that you did not make a typo when entering these pieces of data. You can also double check that you did not forget to create the MySQL user whose credentials you are trying to use to establish the connection by launching the MySQL command line tool with those credentials. Use the command "mysql --user=targetUserName --password=targetUserPassword targetDatabase" in the terminal. In this command "targetUserName" is the MySQL user name you are using to establish the MySQL connection in your PHP code, "targetUserPassword" is that user name's corresponding password, and "targetDatabase" is the name of the MySQL database you are trying to access with that user name. If this command does not work then you need to launch the command line tool as the root user and create the user credentials you are trying to use in PHP.
Accurate Syntax
-
When it comes to programming in any language, small syntax errors can cause problems that the programmer cannot understand. Consequently, if your version of PHP has the necessary MySQL drivers enabled, the MySQL server is running, the credentials you are trying to use are valid, and PHP will still not connect to MySQL then you should go over every character in your "mysql_connect()" command and parameters. Pay special attention to make sure that all the commas are there and you used single quotation marks (') instead of double quotation marks (").
-
References
Resources
- Photo Credit Thinkstock Images/Comstock/Getty Images