Solved

How to connect to multiple database via mysqli?

Posted on 2010-09-08
6
390 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

11 Experts available now in Live!

Get 1:1 Help Now