Cannot Connect MySQL with PHP Scripts

June 6, 2013 / SQL

If you cannot seem to connect to MySQL with a PHP script then it might happen due to the PHP scripts under a domain are not able to connect to the MySQL server. You might have mentioned the connection strings correctly but still not able to connect MySQL through scripts. To sort this error you can do the following steps:

Locate the server’s php.ini.

[root@server ~]# php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini

Here we are considering the server is a su-exe server. Copy php.ini file to the document of the domain.

[root@server ~]# cp /usr/local/lib/php.ini /home/test/public_html

Once you have done this go to the document root of the domain and edit the php.ini file as follows:

[root@server ~]# cd /home/test/public_html

Open the php.ini file and locate the following line:

[root@server public_html]# vi php.ini

Locate for the line:

[SQL]
sql.safe_mode = On

Now change this flag setting to Off

[SQL]
sql.safe_mode = Off

Save the settings and restart the Apache service on the server.

[root@server ~]# /etc/init.d/httpd restart

Through these steps you will be able to connect MySQL with PHP scripts.