PHP multiple MySQL connection though classes/objects

classmysqlobjectphp

Here's the code I'm trying to make work:

<?php

class database {
    var $connection;
    function database($host,$username,$password,$database){
        $this->connection = mysql_connect($host, $username, $password);
        mysql_select_db($database,$this->connection);
    }
    function query($query){
        $query = mysql_query($query,$this->connection);
        return $query;
    }
}

$db = new database("localhost","root","password","database1");
$db2 = new database("SERVER2","root","password","database2");

$sql = $db->query("SELECT * FROM users WHERE name = 'Yifan' LIMIT 1");
$row = mysql_fetch_assoc($sql);

var_dump($row);

$sql = $db2->query("SELECT * FROM users WHERE name = 'Yifan' LIMIT 1");
$row = mysql_fetch_assoc($sql);

var_dump($row);

?>

So, in case you didn't understand that, I want to have two or more connections to mysql using objects, but the problem is, I get "bool(false)" as the first result, and the correct response for the second one. Any idea on what I'm doing wrong, or if it is even possible? Thanks.

Best Solution

The next parameter to mysql_connect is $new_link, can you try sending in a true there and see if it helps?

$this->connection = mysql_connect($host, $username, $password, true);
Related Question