patternphpMinor
Efficient MySQL PDO class
Viewed 0 times
mysqlefficientclasspdo
Problem
I tried to create a secure and efficient PDO class. I would really appreciate your feedback to make it more production-ready.
GitHub
```
dsn = "mysql:host=$this->host;dbname=$this->database;";
if (empty($this->charset)) {
$this->charset = 'UTF8';
}
// Set options
$this->options = array(
PDO::ATTR_EMULATE_PREPARES => false, //prevent false emulation
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
if (!empty($this->port)) {
$this->dsn .= "port=$this->port;";
}
/*
* Ensure charset if set (default is UTF-8)
*/
if (version_compare(PHP_VERSION, '5.3.6') options)) {
$this->options["PDO::MYSQL_ATTR_INIT_COMMAND"] = "SET NAMES '$this->charset'";
}
} else {
$this->dsn .= "charset=$this->charset;";
}
$this->connect();
}
/**
* Connect to database to database if we are not yet connected
*/
private function connect()
{
if ($this->isConnected == false) {
try {
$this->_connection = new \PDO($this->dsn, $this->user, $this->pass, $this->options);
$this->isConnected = true;
} catch (PDOException $e) {
/*
* Couldn't connection to database , show error message
*/
die('Connection failed: ' . $e->getMessage());
}
}
}
/**
* @param $query Set the database query
*/
public function query($query)
{
$this->connect();
$this->stmt = $this->_connection->prepare($query);
}
/**
* @param $options Allows users to parse their own options
* $options must be an array
*/
public function setOptions(array $options) {
if ($this->isConnected) {
$this->CloseConnection();
} else {
$this->options = $options;
$
GitHub
```
dsn = "mysql:host=$this->host;dbname=$this->database;";
if (empty($this->charset)) {
$this->charset = 'UTF8';
}
// Set options
$this->options = array(
PDO::ATTR_EMULATE_PREPARES => false, //prevent false emulation
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
if (!empty($this->port)) {
$this->dsn .= "port=$this->port;";
}
/*
* Ensure charset if set (default is UTF-8)
*/
if (version_compare(PHP_VERSION, '5.3.6') options)) {
$this->options["PDO::MYSQL_ATTR_INIT_COMMAND"] = "SET NAMES '$this->charset'";
}
} else {
$this->dsn .= "charset=$this->charset;";
}
$this->connect();
}
/**
* Connect to database to database if we are not yet connected
*/
private function connect()
{
if ($this->isConnected == false) {
try {
$this->_connection = new \PDO($this->dsn, $this->user, $this->pass, $this->options);
$this->isConnected = true;
} catch (PDOException $e) {
/*
* Couldn't connection to database , show error message
*/
die('Connection failed: ' . $e->getMessage());
}
}
}
/**
* @param $query Set the database query
*/
public function query($query)
{
$this->connect();
$this->stmt = $this->_connection->prepare($query);
}
/**
* @param $options Allows users to parse their own options
* $options must be an array
*/
public function setOptions(array $options) {
if ($this->isConnected) {
$this->CloseConnection();
} else {
$this->options = $options;
$
Solution
You should never do this in production:
Instead, take a look at the PHP SPL exceptions and throw one. If there's not one there you think supports your idea, create your own.
die("You need to change to a storage engine such as InnoDB as MyISAM storage engine does not support transaction.");Instead, take a look at the PHP SPL exceptions and throw one. If there's not one there you think supports your idea, create your own.
Code Snippets
die("You need to change to a storage engine such as InnoDB as MyISAM storage engine does not support transaction.");Context
StackExchange Code Review Q#125934, answer score: 3
Revisions (0)
No revisions yet.