ARGH! That was uttered quite a few times while trying to edit a website on a local Windows XP machine that needed to connect to a mySQL server on a remote machine. Shouldn’t be too tough, right? Wrong …

Ok, so now that you’ve clicked the more link (hehe) here’s the situation.

- IIS is running locally on Windows XP Professional SP2
- PHP5 has been installed in C:\php
- php_mysql.dll is located in C:\php\ext
- php.ini is located in C:\php
- mySQL is running remotely on a machine with IP address 172.16.6.5
- The website root for local IIS is configured as c:\website
- The mySQL configuration file, my.cnf, is in /etc/mysql
- The remote server is running Ubuntu 7.0.4

First I tried to make a file called test.php in C:\website. Aside from the standard HTML code it contains only the following code.

<?php
$link = mysql_connect(’172.16.6.5′, ‘user’, ‘password’);
if (!$link) {
die(’Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

This code should attempt to connect to the mySQL server through PHP and print a message saying it was successful if everything goes ok. Sounds simple? Yes, with a few changes. The steps I went through were as follows.

1. Make sure php.ini exists in C:\php. The default archive contains a file called php.ini-recommended - you can rename this to php.ini for these steps to work although you might want to customise it later.
2. In C:\php\php.ini uncomment the line that reads ;extension=php_mysql.dll so that it reads :

extension=php_mysql.dll

3. In C:\php\php.ini change the line that sets the extension_dir variable so that it reads :

extension_dir=C:\php\ext\

4. Open Control Panel, open System, click the Advanced tab and click Environment Variables. Find the PATH variable in the list called System Variables, click Edit and add the following to the contents of the Variable value box then click OK when done.

;C:\php

Environment Variables

5. While still in the Environment Variables box click New, enter PHPRC as the variable name and enter C:\php as the Variable value. Click OK when done.

Create New Environment Variable

6. Restart IIS by running “iisreset” from a command prompt.

7. On the mySQL server itself you now need to edit the my.cnf file to tell the machine to listen on the network IP address instead of just localhost (127.0.0.1). Using your favourite editor change the line that reads :

bind-address = 127.0.0.1

and change it to read :

bind-address = 172.16.6.5

Note that this will be different for your setup unless your mySQL server has the same IP address as me.

8. Restart mySQL. On my system I used ’su’ to change to the root context and ran the following commands :

mysqladmin shutdown
mysqld &

Now it should all work!