Configuring PHP to communicate with MySQL

Here I would show you how to configure your PHP with MySQL. I am using:

- Apache 2.2.6,
- PHP 5.2.5
- MySQL 5.0.25
- Windows XP
- web server root is c:\www

It is assumed that you have read the previous three articles where I show how to install Apache, install PHP, configure Apache and PHP to communicate with each other and install MySQL.

I have installed PHP on c:\php and the Apache document root is c:\www.

Configuring PHP for MySQL

To configure PHP to communicate with MySQL, we need to edit the php.ini file which, on my computer, is located at c:\php\php.ini.

Edit php.ini
First of all create a backup copy of php.ini. Then edit it to remove the semicolon from

;extension=php_mysql.dll

This would uncomment this line. On my computer, php_mysql.dll file is located at c:\php\ext\php_mysql.dll. This directory is defined in php.ini file:

extension_dir = extension_dir = "c:\php\ext"

Please verify that php_mysql.dll is actually residing at this address. If it is not, then with change this address to where it is located or copy the file to this folder. Otherwise you will get an error like 'cannot connect to mysql_connect()'.

Setting system path
Once you have made this simply but crucial modification, you need to add the php directory to the system path:

Start > Control Panel > Advanced > Environment Variables > System Variables > Path > Edit

Go to the end of the line and append ;c:\php. You need to restart your computer for this to take effect. Once your computer is restarted, go back to verify that the change is permanent.

Testing Configuration

Now we need to test if we can access the database from the web.

Start MySQL client
Start MySQL:

Start > All Programs > MySQL > MySQL Server 5.0 > MySQL Command Line Client

type your password

Create New User
You do not want to give root access to a web client, so we need to create a new user account with limited privileges; select, insert, update, and delete on one database called test.

mysql> create user 'web_user'@'localhost' identified by 'password';
mysql> grant select, insert, update, delete on test.* to web_user identified by 'password';

Create and fill MySQL table
We need to create a table with dummy data to use for testing purposes. First we choose database. Then we create a table. Finally, we fill in some data.

mysql> use test;
mysql> create table person (id int primary key auto_increment, name varchar(30));
mysql> insert into person values (null, 'Alice');
mysql> select * from person;

Write PHP script

<?php
$con = mysql_connect("localhost", "web_user", "password") or die(mysql_error());
mysql_select_db("test",$con) or die(mysql_error());

$q = "SELECT * FROM person";
$rs = mysql_query($q);

while ($field = mysql_fetch_array($rs))
{
$id = $field['id'];
$name = $field['name'];
print 'ID: ' . $field['id'] . ' - Name: ' . $field['name'];
}
}
?>

Save this file as mytest.php inside c:\www\ folder. Reminder c:\www is the Apache document root.

Run the test
Now open your browser and type http://localhost/mytest.php. You should see ID:1 - Name:Alice.