"Unable to connect to the database server at this time" - help please!


I am very new to the world of MySQL and PHP.

I am following Kevin Yank’s online version of “Build your own Database Driven Website using PHP & MySQL”. So far, I have been able to connect to MySQL and create the joke database. PHP also seems to be working fine.

The problem comes when I try to publish MySQL data on the web. I am using the code suggested at

This is the first part of the code:


// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');
if (!$dbcnx) {
 exit('<p>Unable to connect to the ' .
     'database server at this time.</p>');

// Select the jokes database
if (!@mysql_select_db('ijdb')) {
 exit('<p>Unable to locate the joke ' .
     'database at this time.</p>');


When I try to connect as root, using the password I’ve been using to connect to MySQL, this error message comes up: “unable to connect to the database server at this time”.

However, when I try to connect as another user, which I’ve created on MySQL, the error message I get is: “unable to locate the joke database at this time”.

Why am I not able to connect as “root”?

Thanks in advance for your help.


change both exit(…) lines to die(mysql_error())

ok, so I did that and the message I got was:
Access denied for user: ‘root@localhost’ (Using password: YES)

that means that the password you have specified is wrong. can you log in to the mysql client with that password?

Yes, I can. That’s what is so confusing.

ok, log in to the mysql client and run this query:

select host
     , case when password('mypasswd') = password then 1 else 0 end
  from mysql.user
 where user = 'root'

but replace mypasswd with your actual password. post the results here.

Here are the results:

host | case when password(‘xxxxxx’) = password then 1 else 0 end

local host | 1 | 1

The "|"s represent a vertical line in the table.

Thanks a lot for your help longneck.

is there actually a space in the host, or did you mis-type that?

Sorry, not sure what you mean. The results were two columns: host and case when password …etc.

Under the first column; localhost and Under the second column; 1 and 1.

When I initially typed in the results, then previewed the post, it deleted all the “multiple spaces” - making the columns look out of line. So, yes, there was a space after host, but that was deleted. Is that what you mean?

no, i was talking about “local host”, which should be “localhost”.

try this, log in to mysql and issue this command: GRANT ALL ON . TO ‘root’@‘localhost’, ‘root’@‘’ WITH GRANT OPTION

caution: if this isn’t your server, you administrator will be very mad if they set it up specifically to not give these types of privilege to the root user.

Oh… I get it. Yes, “local host” was a typo.

Anyway, I tried the grant command and I am still getting the same message - ie access denied.

what is the command line you are using to log in to mysql? EXACTLY. except replace the password with xxxx if you’re specifying it on the command line.

mysql -u root -p

i’m stumped. have you tried instead of localhost in your PHP file?

Yeah, I have, and this hasn’t worked either. Don’t worry about it too much, I’m sure I’ll figure it out eventually. Probably just a minor oversight.

Thanks a lot for your help longneck.