PHP 4 and MySQL 5

If you followed my adventures with PHP, you know that I tried some tests of my plugins with the version 4 of PHP. What you don’t know is how many time I spent before tests, on a well know problem that I always forget: the incompatibility between PHP 4 et MySQL 5. If you forgot also this problem, I give you the solution to avoid some hours of search.

The story is that MySQL changes its authentication method between releases 4 and 5. The PHP 4 libraries are still using the old method.

Then, if you are trying to connect to a MySQL 5 database with PHP 4, you just get the beautiful following message: Error establishing a database connection.
The solution is simple: you have to change authentication method of MySQL. You can:

  • change setting globally wen launching the database engine,
  • change password only for a specific user.

In the first case, you have to start the database engine with the parameter --old-passwords. For example: mysqld --old-passwords.

There are several methods for the second case. For all of these methods, you have to use the mysql tool:

  • if you are connected with the account that you want to change the password, the command is:
    1
    
    SET PASSWORD = OLD_PASSWORD(<mot de passe>);
  • If your are in the root account, and you want to change password of an another account, the syntax is:
    1
    
    mysql>SET PASSWORD FOR 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
  • You can also use:
    1
    2
    
    mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd') WHERE Host = 'some_host' AND USER = 'some_user';
    mysql> FLUSH PRIVILEGES;

I use Xamp as development platform. This application is very useful, because it includes a MySQL database engine, an Apache server with extensions such as PHP 4 and 5. I don’t advise the last version because PHP 4 is no more included.

To switch from PHP 4 to PHP 5, XAMPP gives us a script (php_switch.bat with Windows, and php_switch.sh with Linux). I added the command in this script to avoid manual operations each time I need to switch.

Conclusion

I hope this tips will be useful for you. For me, I just hope I will remember it for the next time.