Page 1 of 1

Opening of two MySQL connections...

Posted: Mon May 05, 2008 6:06 am
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?

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

Posted: Thu May 08, 2008 5:29 pm
by Jade
You're getting that error because the database test doesn't have a table called tc_settings

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

Posted: Thu May 08, 2008 5:35 pm
by John Cartwright
Not to be rude, but we encourage our users to not post titles such as "URGENT".. everyone is equally urgent :)

Thanks.

Re: Opening of two MySQL connections...

Posted: Thu May 08, 2008 9:39 pm
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.