سؤال

I suddenly gοt this:

Warning: mysql_connect(): in C:\xampp\htdocs\login.php on line 12 cannot connect to server 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.

I use this code:

$dbhost="localhost";
$dbusername="dbusername";
$dbpassword="dbpassword";
$db_name="database";

mysql_connect("$dbhost", "$dbusername", "$dbpassword") or die("cannot connect to server". mysql_error()); 
mysql_select_db("$db_name") or die("cannot select db". mysql_error());

Changing host to:

$dbhost="127.0.0.1";

everything works fine again.

Why did it happen? How can I fix it to work as before? Thank you!

هل كانت مفيدة؟

المحلول

Are you using Linux? Then it happens because of the sockets. If you specify 127.0.0.1 PHP uses TCP/IP communication. If you type in localhost it tries to communicate with the socket (mysql.sock). It is possible that MySQL is not configured to support them or your system is blocking PHP from accessing the socket file.

EDIT: Possibly related: mysql_connect (localhost / 127.0.0.1) slow on Windows platform

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top