473,657 Members | 2,397 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 1977
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
1559
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). Each of these services has a init script in /etc/init.d and a corresponding configuration file in /etc/sysconfig. We use the 'chkconfig' utility to decide if each of those services will be activated on boot or not (it manipulates links under...
1
5449
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 not, the domain that goes right to is is www.freddyswebgraphics.com, which I have tried as well), and everytime I try to test the connection I get this error message. Test connection failed because of an error in initializing provider....
0
3246
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 instantiate two adoDB connection like this $db1 = &NewAdoConnection ("mysql"); $db1 = &NewAdoConnection ("oracle"); then you can run queries simultaneously from these connections. but if
1
1217
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 nothing happens. Then I tried "add data source" and same thing. Is this /unimplemented/ functionality?
3
3532
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 database to connect single application . Please send me your suggestion . Thanks vssp
3
9112
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: TITLE: Connect to Database Engine ------------------------------ Cannot connect to MARION\SQLEXPRESS. ------------------------------ ADDITIONAL INFORMATION:
0
1965
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 child of main form not working. Main form stil working but when try to open child a message show as Please select a valid SQL statement as "Select", "Delet" or "Updat" I am working with Access . I connect database in...
4
2382
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 when I click on Refresh. I know that 2 instances exist. I see them Management Studio. I think this is preventing the listing of local instances using SMO. -- L. A. Jones
0
1324
by: Wendi Turner | last post by:
ASP.NET 2.0 C# WebService Connect to Database: Sql Server 2005 --------------------------------------------------------------------------------------------------- I have successfully created a webservice in ASP.NET 2.0 C# with VS 2005 & VS 2008, however, i can find nothing - books or otherwise to help with the webservice connection to the database. Can anyone please offer help on what to return in the method that makes a select...
2
3827
by: Wendi Turner | last post by:
ASP.NET 2.0 C# WebService Connect to Database: Sql Server 2005 --------------------------------------------------------------------------------------------------- I have successfully created a webservice in ASP.NET 2.0 C# with VS 2005 & VS 2008, however, i can find nothing - books or otherwise to help with the webservice connection to the database. Can anyone please offer help on what to return in the method that makes a select...
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
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 we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.