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!

Related Posts

6 Comments to “Tip: Testing Your PHP/MySQL Connection”

  • faz May 11, 2009 at 5:27 pm

    Hi, I’ve some problem here; I already install PHP 5, apache 2 and mysql. But the problem is I cannot make a connection between PHP and mysql. Why it happen?? I had tested with your coding but it asks me to save or open the testmysql.php again.

  • Rai May 11, 2009 at 11:06 pm

    Did you type in your correct password to your database? What exact error message are you getting?

  • SKANS September 23, 2009 at 11:49 am

    pinoy ba ang may ari ng tutorial na ito?
    naghahanap kasi ako ng pwedeng mag install ng PHP para sa gagawin ko sanang adult site na kagaya ng porntube atbp.
    yung ma install na lahat na kaylangan at ready na siya sa uploading, ang konting modification nalang ay paglagay ng ads, header banner.at simple modification lang… hindi kasi ako marunong ng kahit na ano tingkol sa PHP o MySQl.
    tutl nag bibigay nmn ng tutorial regarding s mga ganitong script, bakit di na lang pagkakaitaan kung may gusto di ba? kung tumatangaap ka ng paypal fee o kung bank account payment.. paki email ako kung interesado ka.

  • deepti February 15, 2011 at 7:04 pm

    give error connection not made is output
    everything is rigth but still i m getting error
    Plz help me

  • RichJ December 26, 2011 at 12:41 pm

    I stumbled across your article when trying to setup Joomla on my local host.

    Trying the php test from cmd line led me to discover the password encryption in my mysql db was 16 bytes, instead of the 41 bytes PHP is looking for. In PHP 5.3 support for 16 byte passwords was deco’d, causing Joomla to fail on PHP connection as well as PhpMyAdmin.

    I guess it’s user error not reading the upgrade docs. :) I had to migrate the date from my older mysql db (which still worked fine) into a newly created db that supported the 41 byte passwords. Setting passwords for each user created a new 41 byte hash. Once I loaded the sql data in, flushed privs, everything worked.

    Thanks for the article. It really helped me pinpoint the problem.

Post comment

CommentLuv Enabled