Solved

How to connect to multiple database via mysqli?

Posted on 2010-09-08
6
391 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
  • 4
  • 2
6 Comments
 
LVL 108

Expert Comment

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now