Solved

How to connect to multiple database via mysqli?

Posted on 2010-09-08
6
407 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to dynamically set the form action using jQuery.

632 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