0

I tried to alter OOP script presented here:

to handle two databases at once (depending on function needs), but it seems not to work at all. I get an error message: mysqli_query() expects at least 2 parameters, 1 given in ...DAL.php on line 60

private function dbconnect($usedb)
{
    switch($usedb)
    {
        case '':
        echo "Error choosing database to operate on.";
        break;
        case '1':
        $conn = new mysqli(DB1_HOST, DB1_USER, DB1_PASSWORD,DB1_DB) or die ("<br>Could not connect to MySQL server");
        //mysqli_select_db(DB1_DB,$conn) or die ("<br>Could not select the main database.");
        return $conn;
        break;
        case '2':
        $conn = new mysqli(DB2_HOST, DB2_USER, DB2_PASSWORD,DB2_DB) or die ("<br>Could not connect to MySQL server");
        //mysqli_select_db(DB2_DB,$conn) or die ("<br>Could not select the client database.");
        return $conn;
        break;
    }
}

private function query($usedb,$sql)
{
    $this->dbconnect($usedb);  
    $res = mysqli_query($sql);

Can this work eventually? How to handle two databases at once if called from a single function that is making querying much easier? What I do wrong?

jakubplus
  • 307
  • 4
  • 17

3 Answers3

1

This piece of code:

private function query($usedb,$sql)
{
$this->dbconnect($usedb);  
$res = mysqli_query($sql);

Should be like:

private function query($usedb,$sql)
{
$conn = $this->dbconnect($usedb);  
$res = mysqli_query($conn,$sql);

mysqli_close($conn); //dont forget to close it

And in you dbconnect function you should use procedural style:

return $link = mysqli_connect("localhost", "my_user", "my_password", "world");
Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • Well, the worst problem is, that even now, I get error like: "mysqli_select_db() expects parameter 1 to be mysqli, string given" How constant, same as first database name, can be a string? Should I convert this to an object? – jakubplus May 17 '13 at 16:47
  • are you using object style on yur function and procedural style outside....you change your code like the return $link y posted on my answer? – Hackerman May 17 '13 at 16:52
  • Robert Rozas, you've been almost exact with your answer. This totally works for me: private function query($usedb,$sql) { $conn = $this->dbconnect($usedb); $res = $conn->query($sql); – jakubplus May 18 '13 at 10:27
1

You don't reconnect to the database every time, you save the link. This is one way to do it.

$dbs= array(1=>dbconnect(1), 2=>dbconnect(2));
private function query($usedb,$sql)
{
     global $dbs;
     return mysqli_query($dbs[$usedb], $sql);
}
Adder
  • 5,708
  • 1
  • 28
  • 56
0

When you use it procedurally like that:

$res = mysqli_query($sql);

It expects: (SRC: http://php.net/manual/en/mysqli.query.php)

 mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

So, you're missing the first parameter.

Since you're returning the object. I would change your private function 'query' to:

$mysql = $this->dbconnect($usedb);
$res = $mysql->query($sql);
asafreedman
  • 572
  • 4
  • 10