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**
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**
Code:
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);
}
}
Comment