views:

140

answers:

4

Hi I have the following PHP code to connect to my db.

<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");

?>

however I get the following error:

Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files (x86)\EasyPHP-5.3.2i\www\checklogin.php on line 11

Warning: mysql_connect() [function.mysql-connect]: 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 (x86)\EasyPHP-5.3.2i\www\checklogin.php on line 11

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\EasyPHP-5.3.2i\www\checklogin.php on line 11

I am able to add a db/tables via phpmyadmin but I cant connect using php.

here is a screenshot of my phpmyadmin page: http://img294.imageshack.us/img294/1589/sqls.jpg any help would be appreciated, thanks in advance.

A: 

Have you created a database yet? Your phpMyadmin page is showing "no databases".

grokus
that wouldn't affect him connecting
Galen
+1  A: 

Check the following:

  1. Is MySQL running?
  2. Is there any firewall that could be blocking your computer from accepting connections to MySQL on port 3306?
  3. Is MySQL listening on port 3306, or did it get changed to something nonstandard?
  4. An odd one, but try changing from localhost to 127.0.0.1

Basically, the errors you're getting mean that it cannot connect to the server. It sends request to localhost:3306, and only waits so long for a reply. it's not getting it, which means the request is either blocked (firewall) or ignored (MySQL is not running and/or is listening on a different port)

If phpMyAdmin came with the MySQL install, then it could be that it was configured to use the appropriately different port

Slokun
#4 seemed to work, Thanks.
ajoe
@user332817 If this is the case, you should take a look in your hosts file, `c:\windows\system32\drivers\etc\HOSTS` on Windows, and make sure it has a line reading something like `127.0.0.1 localhost`. This will make sure that entering `localhost` actually means mapping to your local host.
Slokun
A: 

Maybe your MySQL is not using TCP for localhost. Please try

$host='/tmp/mysql.sock';

or whatever socket it might be using.

ZZ Coder
A: 

changing from localhost to 127.0.0.1 worked, thanks guys.

ajoe
When somebody answers your question, you shouldn't post an answer saying one of them worked. A comment to the answer (as you did) works, but you need to make sure you click the big check mark next to it to accept it, and say it worked.
Slokun