Tip: Testing Your PHP/MySQL Connection

How to test if PHP connects to MySQL database?

There are numerous times when I have to setup Apache and PHP on a freshly installed Linux server. Considering that not only that Apache service should work, I also have to make sure that PHP and MySQL are working properly together.

Forgetful that I am, I always tend to forget what components should I have to make PHP and MySQL talk to each other. You need something like a ‘special glue’ to connect the two together and I will discuss here how to make PHP and MySQL work together for both RPM and source installation.

RPM Installation:

If you are using RPM packages to install your Apache, PHP and MySQL, just run the following commands in the command line (you will need root privileges for this):

yum install httpd mysql mysql-server php

This will install the basic packages for your LAMP setup. However, you need a few more components to get the PHP and MySQL to connect:

yum install php-mysql

php-mysql is a component that glues the PHP and MySQL together. Without this, your PHP installation will not be able to connect to your MySQL databases.

Source Installation:

Installing PHP by source is as easy as doing it in RPM. To begin the installation, you must compile your Apache service before you start with the PHP

installation. When you are done with your Apache installation, you can begin installating PHP by downloading the package:

PHP 4.4.4+Apache 1.4

cd /usr/src
wget http://museum.php.net/php4/php-4.4.4.tar.gz
tar zxf wget http://museum.php.net/php4/php-4.4.4.tar.gz
cd php-4.4.4

(with source mysql)

./configure –enable-inline-optimization \
–with-config-file-path=/usr/local/apache/conf \
–enable-memory-limit –enable-calendar –enable-sysvsem –enable-sysvshm \
–enable-track-vars –enable-trans-sid –disable-debug –disable-static \
–enable-bcmath –enable-ctype –enable-exif –enable-filepro \
–enable-ftp –without-pgsql –enable-shmop –enable-sockets \
–enable-wddx –with-zlib –enable-force-cgi-redirect \
–with-apxs=/usr/local/apache/bin/apxs –with-openssl
–with-mysql=/usr/local/mysql –with-xml \

(with rpm mysql)

./configure –enable-inline-optimization
–with-config-file-path=/usr/local/apache/conf \
–enable-memory-limit –enable-calendar –enable-sysvsem –enable-sysvshm \
–enable-track-vars –enable-trans-sid –disable-debug –disable-static \
–enable-bcmath –enable-ctype –enable-exif –enable-filepro \
–enable-ftp –without-pgsql –enable-shmop –enable-sockets \
–enable-wddx –with-zlib –enable-force-cgi-redirect \
–with-apxs=/usr/local/apache/bin/apxs –with-openssl
–with-mysql –with-xml


./configure –with-config-file-path=/usr/local/apache/conf –with-apxs2=/usr/local/apache/bin/apxs –enable-calendar –enable-ftp –without-pgsql –with-zlib –with-openssl=/usr/local/ssl –with-mysql

make install

Configure MySQL

Now that you have installed the packages, you must first setup your MySQL database root password by doing:

mysqladmin -uroot -p password NEWPASS

You can test you MySQL database by logging in to your database:

mysql -uroot -p

Testing PHP and MySQL

If you can login to your MySQL Database, then you are ready to test PHP and MySQL if they are working together. Test first if your PHP is working fine. You can do this by running this in CLI:

php -i

You should see a bunch of messages that tells information about your PHP installation. What you are looking for is the line that contains the information about your MySQL installation. You can do this by executing:

php -i | grep -i mysql

And you should see something like this:

MySQL Support => enabled
MYSQL_SOCKET => /var/lib/mysql/mysql.sock
MYSQL_INCLUDE => -I/usr/include/mysql
MYSQL_LIBS => -L/usr/lib/mysql -lmysqlclient

When you have that, then you are good to go!

One last thing is that you should test the connection by creating a PHP script that connects to your MySQL database. I have one that I always use and this is how it looks like:


< ?php
$dbhost = ‘localhost’;
$dbuser = ‘root’;
$dbpass = ‘NEWPASS’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);

if ($conn) {
echo “CONNECT OK”;

$dbname = ‘mysql’;

You should put this file to where you set your Document Root in your Apache config so you can open it with your browser. Or you can also run this script in the CLI by doing:

php php-test.php

If you see the words “CONNECT OK”, then congratulations! You have successfully installed Apache, PHP and MySQL on your Linux server. If not, then you should see the error messages that will be displayed.

If you encounter any errors, you can ask me by posting a comment and I will try to help you out with your installation, free of charge!