PHP can't connect to MySQL

Hello everyone,

I am working through Kevin Yank’s book “Build Your Own Database-Driven Web Site”, and I ran into a problem with the deletejoke file, which has been previously documented:

At the suggestion of the forum members, I decided to uninstall PHP 5.2 and go with 5.3, according to the directions in the book as well as the updates/errata page for the book:

Now the server times out whenever I try to connect to the database. (Other aspects of PHP such as datetime and include files is working okay.) I’m not sure what the problem is.

When I was running PHP 5.2 previously, I could connect to the server all right, but launching deletejoke would cause the server to crash.

Here is the error message I am getting in my browser now:

================

Warning: mysqli_connect() [function.mysqli-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

Is MySQL running?

Yes, MySQL is working fine.

Did you configure MySQL to use a port other than 3306, or have any software running that would block said port?

No. And here is the strange thing: since I installed PHP manually, I dug up the old 5.2 version, and when I run that, it connects to the database fine (the problem is that the server crashes once I get to more complex examples like deletejoke).

But when I try PHP 5.3, I can’t establish a connection to the database at all.

I am running Apache 2.2.13 on Windows Vista.

dump phpinfo(); in 5.3 and check the mysqli block’s configuration?

Please excuse my newb, but by dump I am assuming that you want me to put the phpinfo(); function into a file and run it in a browser? I am attaching a screenshot of the results.

If you don’t feel like messing around with the attachment, here is the text, copied and pasted:

========================

mysqli
MysqlI Support enabled
Client API library version mysqlnd 5.0.7-dev - 091210 - $Revision: 294543 $
Active Persistent Links 0
Inactive Persistent Links 0
Active Links 0

Directive Local Value Master Value
mysqli.allow_local_infile On On
mysqli.allow_persistent On On
mysqli.default_host no value no value
mysqli.default_port 3306 3306
mysqli.default_pw no value no value
mysqli.default_socket no value no value
mysqli.default_user no value no value
mysqli.max_links Unlimited Unlimited
mysqli.max_persistent Unlimited Unlimited
mysqli.reconnect Off Off

I have figured out the answer to the problem I was experiencing. I am documenting below, just in case it helps anyone else.

=========================

I recently came across a problem when working through the 4th edition of Kevin Yank’s “Build Your Own Data-Driven Website.”

There appears to be an incompatibility between certain software packages and operating systems. In particular, this affects PHP 5.3 when trying to connect to MySQL while running on Windows Vista with Apache. It is not really a bug, but a discrepancy between how far along the different elements are in the transition from IPv4 to IPv6.

Basically, when PHP tries to connect to the database, it times out because it does not recognize localhost, only 127.0.0.1, and produces the following error message:

=========================

Warning: mysqli_connect() [function.mysqli-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2

=========================

There are two possible solutions to this problem:

=========================

  1. Replace “localhost” with “127.0.0.1” in all PHP files that you wish to have connect to a MySQL database

  2. Locate the “hosts” file on your computer. It is typically at a location such as this:

C:\Windows\System32\drivers\etc\hosts

Open it up, and comment out the line that prevents the localhost from “mapping” correctly; in otherwords, change

::1 localhost

to

#::1 localhost

This solution was, in fact, presented in the SitePoint forums previously, though without a full explanation of the details.

=========================

The issue is described in detail on the PHP forum at the following link:

http://bugs.php.net/bug.php?id=45150

Please note that – from what I understand – Windows XP does not have this problem, as it is configured only to IPv4. Vista runs into a problem because it is designed to handle both, as is Windows 7.

Also, I should mention that using PHP 5.2 with the aforementioned book is not recommended because there is a bug in that version – a veritable one – that causes Apache to crash when it attempts to open the “deletejoke” file.