patternphpMinor
Entire PDO DB Class
Viewed 0 times
classentirepdo
Problem
Last time I asked a question here, it was for my select function. In that question, another user stated the way I did it, might not have been safe. So here I am with the entire class.
The class I'm about to show you is going to be used for a client's Homepage. My main concern is the safety of the PDO Class; I want to make sure that it's safe to use. Furthermore, if there are any coding improvements I'd really like to hear them, because every comment about my code makes me better.
```
class DB {
private static $_instance = null,
$_vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y");
private $_pdo,
$_query,
$_results,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mysql:dbname='. Config::get('pdo/db') .';host='. Config::get('pdo/host'), Config::get('pdo/username'), Config::get('pdo/password') );
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_pdo->exec("SET NAMES 'utf8';");
}
catch(PDOException $e) {
die($this->ExceptionLog($e->getMessage(),$type="Database Connection"));
}
}
public static function getInstance() {
if(!isset($this->_instance)){
$this->_instance = new DB();
}
return $this->_instance;
}
private static function removeVowelsWhere(array $array, $table) {
$removed = array();
foreach($array as $key => $value)
{
foreach($value as $operator => $attribute)
{
$removed[] = str_replace($this->_vowels, "", $table) . "." . $key . " " . $operator . " '" . $attribute . "'";
}
}
return $removed;
}
private static function removeVowels(array $array) {
$removed = array();
foreach($array as $key => $value
The class I'm about to show you is going to be used for a client's Homepage. My main concern is the safety of the PDO Class; I want to make sure that it's safe to use. Furthermore, if there are any coding improvements I'd really like to hear them, because every comment about my code makes me better.
```
class DB {
private static $_instance = null,
$_vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y");
private $_pdo,
$_query,
$_results,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mysql:dbname='. Config::get('pdo/db') .';host='. Config::get('pdo/host'), Config::get('pdo/username'), Config::get('pdo/password') );
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_pdo->exec("SET NAMES 'utf8';");
}
catch(PDOException $e) {
die($this->ExceptionLog($e->getMessage(),$type="Database Connection"));
}
}
public static function getInstance() {
if(!isset($this->_instance)){
$this->_instance = new DB();
}
return $this->_instance;
}
private static function removeVowelsWhere(array $array, $table) {
$removed = array();
foreach($array as $key => $value)
{
foreach($value as $operator => $attribute)
{
$removed[] = str_replace($this->_vowels, "", $table) . "." . $key . " " . $operator . " '" . $attribute . "'";
}
}
return $removed;
}
private static function removeVowels(array $array) {
$removed = array();
foreach($array as $key => $value
Solution
There's a few changes I'd suggest to insert, firstly as the second parameter has to be an array, you can use type hinting to indicate this:
You can also generate the
The only other comment I have is that the
public function insert($table, array $fields)You can also generate the
$values without using a foreach loop, like so:$values = substr(str_repeat('?, ', count($fields)), 0, -2);The only other comment I have is that the
removeVowels and removeVowelsWhere methods are static, so you should be calling them as self::removeVowels and self::removeVowelsWhere rather than $this-> which is for instance methods.Code Snippets
public function insert($table, array $fields)$values = substr(str_repeat('?, ', count($fields)), 0, -2);Context
StackExchange Code Review Q#52619, answer score: 2
Revisions (0)
No revisions yet.