3

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.

2
  • You might want to consider migrating to mysqli, which is newer and more developed. Commented May 23, 2009 at 1:35
  • Thank you, one of the reason I'm doing classes for SQL connections is to support multiple servers. So class mysqlDB extends Database, class mysqliDB extends Database, and so on. Commented May 24, 2009 at 14:49

1 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.