I have made this class for database connections to my applications. It eases out the task of making connections to the database with knowing the credentials in advance and provides a way to execute the queries.
Class dbConnection {
public $CONN;
private $dbservername, $dbusername, $dbpassword, $dbname;
public function __construct() {
$this->dbservername = "localhost";
$this->dbusername = "dbroot";
$this->dbpassword = "pass";
$this->dbname = "test";
$this->open(); //open the connection on instantization
}
public function open() {
// Create connection
$this->CONN = new mysqli($this->dbservername, $this->dbusername, $this->dbpassword, $this->dbname);
// Check connection
if ($this->CONN->connect_error) {
die("Connection failed: " . $CONN->connect_error);
}
}
public function prepare($query) {
//prepare the data
return $this->CONN->prepare($query);
}
public function close() {
//close the connection
$this->CONN->close();
}
}
I am using prepared statements in the queries, of course.
I would like a review to determine if the function is feasible in all situations with performance and security on top considerations. I want to know if I should make any adjustments to it. Also, a comment on the coding style would be of great use.
mysqli
, with another interface. The simplest purpose of a wrapper should be that you can change the underlying implementation later, and you can't do that with this. Search for simple ORMs or query builders, they will help you better. \$\endgroup\$