PHP Can't Connect to MYSQL as LOCALHOST

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bdrew
Forum Newbie
Posts: 3
Joined: Fri Jul 21, 2006 8:22 am

PHP Can't Connect to MYSQL as LOCALHOST

Post by bdrew »

I'm new to PHP and MYSQL development, and have been experiencing problems getting PHP to connect to MYSQL.

If I use either of the standard connection statements to connect to a database server running locally

$db = mysql_pconnect('localhost', 'php_webuser', 'php123xx');

or

$db = mysql_pconnect('127.0.0.1', 'php_webuser', 'php123xx');

the connection fails.

If however, I state the full network address of the local machine, e.g.

$db = mysql_pconnect('192.168.100.100', 'php_webuser', 'php123xx');

the connection is successful.

Can anyone point me in the direction of why the first method is failing?

Many thanks,

Bryan
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

How is your database set up? I think in the `user` table, you need to state that the user can login from 'localhost' or from '%'.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think you got that right pickle. You can manage the users server by managing permissions. You can do it in phpMyAdmin or in the MySQL Administrator (or whatever commercial app you might use to manage your database). Just set the server that the user name is attached to to either 'localhost' or '%'.
bdrew
Forum Newbie
Posts: 3
Joined: Fri Jul 21, 2006 8:22 am

Post by bdrew »

Thanks guys - although I'm still a little unclear about this.

My database was setup with the offending user set as '%' for his location. This didnt work with 'localhost'.

I have now added 'localhost' as a specific location and all seems to be working ok.

Can't explain why the '%' option was refusing to let the 'localhost' connection from working though.

Bryan
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Everah wrote:You can do it in phpMyAdmin or in the MySQL Administrator (or whatever commercial app you might use to manage your database)
Or actually going into the `mysql` database & goofing around. I'm always a proponent of the 'learn to do it the hard way before the easy way' school. :twisted:


@~bdrew - I seem to remember that happening to me before & I too have no idea why it wouldn't work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What's your server version? That may explain it. I personally have had no experience with that problem so I couldn't tell you one way or the other why that would have happened.

pickle, with guys like you doing it the hard way and teaching guys like us that learn from guys like you, I'd never have to get into mysql from the CL. :wink:
Post Reply