Page 1 of 1

PHP Can't Connect to MYSQL as LOCALHOST

Posted: Fri Jul 21, 2006 8:30 am
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

Posted: Fri Jul 21, 2006 10:53 am
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 '%'.

Posted: Fri Jul 21, 2006 11:00 am
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 '%'.

Posted: Fri Jul 21, 2006 11:56 am
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

Posted: Fri Jul 21, 2006 12:43 pm
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.

Posted: Fri Jul 21, 2006 12:48 pm
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: