Solved

How to connect to multiple database via mysqli?

Posted on 2010-09-08
6
398 Views
Last Modified: 2013-12-13
Hello,

I have created a class when I used to connect to the database using the __construct function.  This class also act as my selecting and executing of query.  I get this working for connecting to one database as I define the database host, username, password and db_name as a constant.  Now I want to somehow be able to pass different database name to this class, how can I do this so that this class continue to work?  For example, my constant DB_NAME is "test database".  I want to contact to another database name "test2 database".  How can I make it work?

My code for my class:

**note the upper case name is define in another file**

class QueryBuilder
{
public $host = DB_HOST;
public $db_username = DB_USERNAME;
public $db_password = DB_PASSWORD;
public $db_name = DB_NAME;

public function __construct()
{
      $this->mysqli = new mysqli($this->host,$this->db_username,$this->db_password,$this->db_name);      
}

public function __destruct()
{
      $this->mysqli->close();
}

public function select($sql)
{
      $qry = $this->mysqli->query($sql);
}

public function query($sql)
{
      $qry = $this->mysqli->query($sql);
}

}
0
Comment
Question by:bigjdve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33628324
Pass the credentials to the constructor as arguments, just like the select and query methods.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33628376
Example...

HTH, ~Ray
<?php // RAY_temp_thing.php
error_reporting(E_ALL);

class QueryBuilder
{
    public function __construct($h, $u, $p, $n)
    {
        $this->mysqli = new mysqli($h, $u, $p, $n);
    }
}

$x = new QueryBuilder (1,2,3,4);

Open in new window

0
 
LVL 3

Author Comment

by:bigjdve
ID: 33628735
Ray_Paseur - the connection is working.  However, in my select or query method, I can not execute the query.

In my select method:

$qry = $this->mysqli->query($sql);

It does seem to recognize the "$this->mysqli".  I have this code for escaping the character
($this->mysqli->real_escape_string($value)) and it does not perform the escaping.

If I echo out the statement that build that sql statement, it does not understand.

For example:

echo $sql = "select * from table where id=".$this->mysqli->real_escape_string($value);

result: "select * from table where id="";

If I remove the escape string, it will properly echo with "select * from table where id=1".

Any ideas why??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33629605
I did not see any escape code in the example above.  Can you please post THE ACTUAL CODE that is causing the issue?
0
 
LVL 3

Author Closing Comment

by:bigjdve
ID: 33629627
This solution does work.  There was a typo in my password.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33629661
Ha!  Glad you got it working, and thanks for the points, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question