Php multiple connection mysql though classes / objects?

Here is the code I'm trying to do:

<?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, if you do not understand this, I want to have two or more connections to mysql using objects, but the problem is that I get "bool (false)" as the first result and the correct answer for the second. Any idea on what I'm doing wrong, or if possible? Thanks.

+3
source share
1 answer

The next parameter to mysql_connect is $ new_link, can you try sending it to the right place and see if it helps?

$this->connection = mysql_connect($host, $username, $password, true);
+7
source

Source: https://habr.com/ru/post/1708923/


All Articles