Connecting to a DB on a different server than the PHP file

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Connecting to a DB on a different server than the PHP file

Post by impulse() »

I have wrote a PHP script that connects to a MySQL server on another IP address but it trying to connect to localhost still. The script is as follows:

Code: Select all

mysql_connect("x", "x", "x");
mysql_select_db("x");

$query = mysql_query("SELECT * FROM x");

while ($results = mysql_fetch_array($query)) {
  echo $results['id'], "<br>";
  echo $results['fName'], "<br>";
  echo $results['sName'], "<br>";
  }
But I receive the following error:
PHP Warning: mysql_connect(): Access denied for user 'database'@'stesbox.co.uk' (using password: YES) in /var/www/html/php/test/db.php on line 3
Stesbox.co.uk is my own box that the PHP file is running on. Can someone advise my of what I've done wrong here please?

Regards,
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

sorry for the dumb question, but did you enter in the new host instead of "localhost"
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Yeah. Sorry, removing the host wasn't a good idea for my post.

I can telnet to the IP on port 3306 fine so the MySQL connection is there but my script seems to want to connect to localhost
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Sorry, ignore my post below, not solved :(

Solved.

If I put the connection strings in ' ' rather than " " it connects fine.[/b]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You had a $ in your password?
(can't imagine another reason)
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Nope, the password is a plain ole string.

I tried using ' ' instead of " " just as a test. I've even tried setting the server address as the reverse DNS entry to the IP of the server but still without any connection success. Would someone mind trying my script on a server and let me know the results please?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Access denied for user 'database'@'stesbox.co.uk' (using password: YES)
The actual connection to the server is working but your credentials are rejected.
see http://dev.mysql.com/doc/refman/5.0/en/ ... enied.html
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

A-ha! It works.

My mistake is that when I was receiving
Access denied for user 'database'@'stesbox.co.uk' (using password: YES)
It looks to me like it was my server (stesbox.co.uk) that was rejecting the password.
Post Reply