PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 14, 2022

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

 January 14, 2022     lamp, mysql, mysql-connect, php     No comments   

Issue

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_connect 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


Solution

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.



Answered By - Brad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing