It will connect to MySQL from outside Ok, but not from inside of the same serve!


We have 2 servers, say:

I am able to connect to the MySQL on server n1 from server s1 ok, that is with command like:

$dbcnx2 = @mysql_connect('', 'root', 'xyz21')

But when I try to connect to the same MySQL from the same n1 server, it will not connect!!!

FYI, I tried this too for connecting to MySQL on server n1 from a Php on server n1, and it still does not work!!!

$dbcnx2 = @mysql_connect('host', 'root', 'xyz21')

WHAT is the problem please?


what is the value of “host”

it should be absolute path “

Some MySql servers are configured to accept connections only from the localhost.

The people in the MySql Forum will have more details about that.


Actually by your response I was able to figure out what the problem
was and what the solution needs to be, that is I should have used
localhost as the host address for mysql connect command rather than host.

However I have another question:

How do you maintain, or better said identify, 2 or more different MySQL
connections in a single Php program?

That is say I connect to server1 via this command:

$dbcnx1 = @mysql_connect('server1, ‘root’, ‘xyz2009’);

and to server 2 via:

$dbcnx2 = @mysql_connect('server2, ‘root’, ‘yzx2009’);

Now how do I specify in a given MySQL command, in that Php program, as to which database to use to read and write?


Read the documentation?

For example, mysql_query takes a second parameter where you can specify the resource to use.


I checked this link but cannot see how one specifies the database to
use in a query!

Can you kindly give an example of how one does this?
That is how does one specify which database connection to use in a
MySQL command when multiple database connections have to be open
in a Php program?


From the description part (right near the top) of the mysql_query page

resource mysql_query ( string $query [, resource $link_identifier ] )

$sql ="INSERT INTO (host) VALUES ('localhost');"

$select_db = mysql_select_db('mysql', $link);

$select_db = mysql_select_db('mysql', $link2);


Thanks, this is working fine.

But just one follow up question:
when one has multiple MySQL connections in a Php program, does one
only specify the MySQL connection in the mysql_query command or in any others too?


Let’s go back to the mysql_query documentation to find out:

If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.

If your using more than one connection you need to specify which one you want to use each time otherwise how does php know which one you intend to use?

If the resource is not specified, the last one that was opened by mysql_connect is used. It’s in the docs.


That was not my question at all.
My question, in case it was not clear last time, is that do we need to specify which MySQL connection to use only in mysql_query() or are there other
Mysql Php commands in which we need to specify which MySQL connection to use when more than one MySQL connection has been opened.


Yes there are, and they are pretty much all mysql commands that interact with the database.

The documentation has full details for each command regarding this issue.

If you don’t specify which link to use, the one that was last opened by mysql_connect will be used.

I personally feel that the link identifier should always be given. If you upgrade to the mysqli extension then it is always mandatory there.

As per above two posters said, you would need the link_identifier for nearly all functions dealing directly with database such as:

Ok. thanx.
But FYI as I have been programming with this since few a days ago,
I can give you this lil tip:
in fact you do not need to specify the db connection but in the
mysql_query command since in all mysql commands in between 2 different mysql commands you end up referring to the data set from the last mysql_query command so same DB connection is used. Although, I am sure there will be some exceptions, this seems to be a pretty solid and useful rule.