473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to connect to multiple database via

3 New Member
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**

Expand|Select|Wrap|Line Numbers
  1. class QueryBuilder
  2. {
  3. public $host = DB_HOST;
  4. public $db_username = DB_USERNAME;
  5. public $db_password = DB_PASSWORD;
  6. public $db_name = DB_NAME;
  7.  
  8. public function __construct()
  9. {
  10.       $this->mysqli = new mysqli($this->host,$this->db_username,$this->db_password,$this->db_name);      
  11. }
  12.  
  13. public function __destruct()
  14. {
  15.       $this->mysqli->close();
  16. }
  17.  
  18. public function select($sql)
  19. {
  20.       $qry = $this->mysqli->query($sql);
  21. }
  22.  
  23. public function query($sql)
  24. {
  25.       $qry = $this->mysqli->query($sql);
  26. }
  27.  
  28. }
Sep 8 '10 #1
1 1965
guillermobytes
77 New Member
the code you give doesn't look quite right, but I assume you gave just a blueprint. If I understand the question you want to use the DB_NAME constant value as the database name, but the constant is defined in another file: use a require_once:
Expand|Select|Wrap|Line Numbers
  1. require_once '/constants_file_name.php';
if the problem is that you want to connect to two databases in the same script, then you are doing it wrong, you should not use constants for defining your QueryBuilder data members.
you could create public static data members and modify the database name before instantiating your QueryBuilder:
Expand|Select|Wrap|Line Numbers
  1. QueryBuilder::$db_name = 'test2_database';
  2. $qB = new QueryBuilder();
or you could pass those to the constructor as parameters, as it is done in mysqli constructor.
Expand|Select|Wrap|Line Numbers
  1. QueryBuilder( $host, $user, $pass, $dbname);
for that you should change your constructor to :
Expand|Select|Wrap|Line Numbers
  1. public function __construct($host, $user, $pass, $dbname)
  2. {
  3.     $this->host = $host;
  4.     $this->user = $user;
  5.     //...
  6.     $this->mysqli = new mysqli($this->host,$this->db_username,$this->db_password,$this->db_name);
  7. }
Sep 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1542
by: Fernando Nasser | last post by:
Multiple database services and multiple versions on Red Hat Linux systems The way it works is that we require a specific service script for each database service (that is listening on each port)....
1
5436
by: Alex Hardin | last post by:
Hey, I'm trying to use the connect to database wizard to connect to a MySQL database on my web server, 66.235.193.45 (I'm actually not sure if that points specifically to my part of the server, if...
0
3241
by: CountDraculla | last post by:
Fixing Multiple Database bug in adoDB popular data access layer for php, adoDB can support multiple databases from different provider at time, but not from same provider. what I mean is if you...
1
1208
by: John Bailo | last post by:
I just loaded c# express and sql05 express. When I try to connect to the database from c#e nothing happens. I tried "connect to database" and when I click on the menu, it just goes away and...
3
3526
by: vssp | last post by:
Hi friends In my application i need to connect multiple database.First i connect one database and got the value then i connect another database to insert the values . If any possible to two...
3
9100
by: mbsparrow | last post by:
I have just installed sql server 2005 Express . The Server is running but I can't connect to the Server in Server Management Studio (on the same machine). I am getting the following message: ...
0
1952
by: asad56 | last post by:
I am workin with a superstore managment project. I connect Access database with main form . Then it work properly. But now I connect same database with another table or field in another form which is...
4
2376
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
This is kind of a cross forum problem. I am trying to Connect to database by clicking on: Tools Connect To Database Select: Microsoft SQL Server (SqlClient). However, there are no server listed...
0
1313
by: Wendi Turner | last post by:
ASP.NET 2.0 C# WebService Connect to Database: Sql Server 2005 --------------------------------------------------------------------------------------------------- I have successfully created a...
2
3817
by: Wendi Turner | last post by:
ASP.NET 2.0 C# WebService Connect to Database: Sql Server 2005 --------------------------------------------------------------------------------------------------- I have successfully created a...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.