Opening of two MySQL connections...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Opening of two MySQL connections...

Post by kaisellgren »

Hello!

I have this wierd problem. I have written a MySQL class that works great, but when I open another connection that uses DIFFERENT connection details, then it is not gonna work.

Example usage:

Code: Select all

$db = new mysql($dbconfig[0]);
$db2 = new mysql($dbconfig[0]);
$db3 = new mysql($dbconfig[0]);
$db4 = new mysql($dbconfig[0]);
$db2 -> query('SELECT name FROM '.PREFIX."settings WHERE id='1';"); // Query Works! :D  

Code: Select all

$db = new mysql($dbconfig[0]);
$db2 = new mysql($dbconfig[0]);
$db3 = new mysql([color=#BF4040]$dbconfig[1][/color]);
$db4 = new mysql($dbconfig[0]);
$db2 -> query('SELECT name FROM '.PREFIX."settings WHERE id='1';");
// SAME CODE, EXCEPT NUMBER 1 IN $db3'S DEFINITION = DOESNT WORK :(
Part of my MySQL class:

Code: Select all

function mysql($config)
   {
    $this -> link = @mysql_connect($config['hostname'],$config['username'],$config['password']) or trigger_error(mysql_error());
    $this -> select_db = @mysql_select_db($config['database'],$this -> link) or trigger_error(mysql_error());
   }
So the problem is, I can define unlimited (not really.. :P) db connections like

Code: Select all

$db = new mysql($dbconfig[0]);
$db2 = new mysql($dbconfig[0]);
And it works sweet, but I can not use different configurations, or it will be messed up:

Code: Select all

$db = new mysql($dbconfig[0]);
$db2 = new mysql($dbconfig[1]); // Config 1 - this will mess up all queries of $db =(
And my configs:
0:
host:localhost
user:root
pass:
database: triton
1:
host:localhost
user:root
pass:
database:test
So this

Code: Select all

$db = new mysql($dbconfig[0]);
$db2 = new mysql($dbconfig[1]);
$db -> query('SELECT name FROM '.PREFIX."settings WHERE id='1';");
Will return:
Table 'test.tc_settings' doesn't exist (Line 34.)
Why is it looking at 'test' db instead of 'triton'? Why does that second configuration mess my first configuration?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Urgent help needed with opening of two MySQL connections...

Post by Jade »

You're getting that error because the database test doesn't have a table called tc_settings
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Urgent help needed with opening of two MySQL connections...

Post by John Cartwright »

Not to be rude, but we encourage our users to not post titles such as "URGENT".. everyone is equally urgent :)

Thanks.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Opening of two MySQL connections...

Post by nowaydown1 »

Can you please post your full MySQL class? I could probably offer up some guidance if I could see the full scope of how your class is constructed.
Post Reply