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

Hello,

We have 2 servers, say:
n1
s1

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

$dbcnx2 = @mysql_connect('www.n1.com', '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?

Regards,

what is the value of “host”

it should be absolute path “www.s1.com

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

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

Hi,

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?

Regards,

Read the documentation?

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

Hi,

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?

Regards,

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

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


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

$link=mysql_connect('host','user','pass');
$select_db = mysql_select_db('mysql', $link);
mysql_query($sql,$link);

$link2=mysql_connect('host','user','pass');
$select_db = mysql_select_db('mysql', $link2);
mysql_query($sql,$link2));

Hi,

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?

Regards,

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.

Hi,

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.

Cheers,

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:
mysql_query
mysql_select_db
mysql_create_db
mysql_close

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.