开发者

LAMP settings – PHP and multiple DB connections with mysql_connect()

I was assigned to maintain prehistoric PHP application, which uses some 'phpDB' class written by Joe Thong, last updated in 1999. This application connects to two different databases on the same server, taking some data from one and another from the second one.

Now, the limit of mysql_connect is, that it uses the same connection resource for new connections. Therefore, if I use following code:

$db1 = new phpDB()->connect(/* db1data, database 'one' */);
$db2 = new phpDB()->connect(/* db2data, database 'one' */);
$data = $db1->query($somequery); 

EDIT NOTE: new phbDB()->connect just sets some internal values and does standard mysql_conn开发者_Python百科ect without $new_link parameter.

Now, the problem is, that the $query is run over the database two, because it has rewritten previous connection.

This can be solved by using true as fourth parameter in mysql_connect. The thing is, I would rather not rewrite something in 12 years old library (because of pure fear of how it will react on live server), and also it works without that fourth parameter on live server. However, me not being server guru, I was unable to locate the proper directive in the server configuration to switch on my local MAMP configuration, to be closer to emulate live enviroment.

Can anyone help me? Thank you.

EDIT: wrapper itself:

phpDB.php – db wrapper – http://scrp.at/wd

phpDB-mysql.php – mysql specific code – http://scrp.at/we


The way to fix this is by modifying that query method.

Somewhere inside it is calling mysql_query(). All you have to do is add a second parameter which includes a reference to the internal variable containing the MySQL connection reference.

This is speculation because you haven't posted any of the code, but I think you will see something like this:

Old

function query($thequery) {
    mysql_query($thequery);
    ....
}

Change it to this

function query($thequery) {
    mysql_query($thequery, $this->dbreference);
    ....
}

Whatever "dbreference" is called in your class, I have no idea. Look for whatever variable is set when mysql_connect is called.


Have a look at the following pages:

http://nl.php.net/manual/en/function.mysql-connect.php

http://nl.php.net/manual/en/function.mysql-query.php

By filling the new_link parameter you can add a new connection.

For example:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password', 'link1');
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password', 'link2');

mysql_query($query, 'link1'); //query on database 1
mysql_query($query, 'link2'); //query on database 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜