Windows 7 and PHP/MySQL

Just spend a few hours getting Windows 7, Apache, PHP and MySQL up and running.

It should have been fairly easy, but there was a small problem.

While all the subparts worked independendly, PHP was unable to connect to the MySQL database.

MySQL was installed on localhost just to make it easy. However, this is were the trouble began. If you look in the hosts file in Windows 7 (c:\Windows\system32\drivers\etc\hosts) you will notice, that an important line has been commented out:

#     localhost

Now since this line is missing/commented out, PHP can’t find the IP for localhost. Remove the # sign, and save the file. PHP should be able to connect just fine now 🙂

As a note, you might run into trouble editing the file. Simply find Notepad in you start menu, right click and select “Run as administrator”. Notepad can now edit and save the changes made to the file.

I hope this blogpost saves someone else a bit of time.

One Response to “Windows 7 and PHP/MySQL”

  • zoonman Says:

    Please, see php.ini, [MySQL] section,
    you may setup mysql.default_host variable to
    And when you try to mysql_connect() define server to

Leave a Reply

You must be logged in to post a comment.