I have recently learned about using the functions exposed by the PDO extension. I created a db class which can handle common actions like create, update, delete and select. Is this method a correct way to connect to the database using prepared statements?
<?php
class Db {
private $db_host = DB_HOST;
private $db_user = DB_USERNAME;
private $db_pass = DB_PASSWORD;
private $db_port = DB_PORT;
private $db_name = DB_DATABASE;
private $db_charset = DB_CHARSET;
private $dsn = "";
private $pdo = null;
private $stmt = null;
private $result = array();
private $conn = false;
private $options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
public function __construct()
{
if(!$this->conn) {
try {
$this->dsn = "mysql:host=" . $this->db_host . ";dbname=" . $this->db_name . ";charset=" . $this->db_charset . ";port=" . $this->db_port;
$this->pdo = new PDO($this->dsn, $this->db_user, $this->db_pass, $this->options);
return $this->pdo;
} catch (PDOException $e) {
$err = "Connection Failed: " . $e->getMessage();
}
}
return false;
}
protected function select($table, $rows = "*", $join = null, $join_side = null, $where = array(), $order = null, $limit = null) {
if($this->tableExists($table)) {
$sql = "SELECT $rows FROM $table";
$paramsArray_where = null;
if($join != null && $join_side != null)
$sql .= " $join_side $join";
if(!empty($where)) {
$table_val_where = ":" . implode(',:', array_keys($where));
// Create an array of new keys that contain : in front of them
$table_key_where = explode(",", $table_val_where);
// get array values
$values_where = array_values($where);
// combine key with their respective values
$paramsArray_where = array_combine($table_key_where, $values_where);
$args = array();
foreach($where as $key=>$value)
$args[] = "$key=:$key";
$sql .= " WHERE " . implode(' && ', $args);
}
if($order != null)
$sql .= " ORDER BY $order";
if($limit != null)
$sql .= " LIMIT $limit";
$this->stmt = $this->pdo->prepare($sql);
if($this->stmt->execute($paramsArray_where)) {
$this->result = $this->stmt->fetchAll();
return true;
}
}
return false;
}
protected function insert($table, $params=array()) {
if(!empty($params)) {
if($this->tableExists($table)) {
// Seperating $params key and values
$table_cols = implode(',', array_keys($params));
$table_val = ":" . implode(',:', array_keys($params));
// Create an array of new keys that contain : in front of them
$table_key = explode(",", $table_val);
// get array values
$values = array_values($params);
// combine key with their respective values
$paramsArray = array_combine($table_key, $values);
$sql = "INSERT INTO $table ($table_cols) VALUES ($table_val)";
$this->stmt = $this->pdo->prepare($sql);
if($this->stmt->execute($paramsArray))
return true;
}
}
return false;
}
protected function update($table, $params=array(), $where=array()) {
if(!empty($params)) {
if($this->tableExists($table)) {
$table_val = ":" . implode(',:', array_keys($params));
// Create an array of new keys that contain : in front of them
$table_key = explode(",", $table_val);
// get array values
$values = array_values($params);
// combine key with their respective values
$paramsArray = array_combine($table_key, $values);
$args = array();
foreach($params as $key=>$value)
$args[] = "$key=:$key";
$sql = "UPDATE $table SET " . implode(', ', $args);
if(!empty($where)) {
$table_val_where = ":" . implode(',:', array_keys($where));
// Create an array of new keys that contain : in front of them
$table_key_where = explode(",", $table_val_where);
// get array values
$values_where = array_values($where);
// combine key with their respective values
$paramsArray_where = array_combine($table_key_where, $values_where);
$bind_params = array_merge($paramsArray, $paramsArray_where);
$args = array();
foreach($where as $key=>$value)
$args[] = "$key=:$key";
$sql .= " WHERE " . implode(' && ', $args);
}else{
$bind_params = $paramsArray;
}
$this->stmt = $this->pdo->prepare($sql);
if($this->stmt->execute($bind_params))
return true;
}
}
return false;
}
protected function delete($table, $where = array()) {
if($this->tableExists($table)) {
$sql = "DELETE FROM $table";
$paramsArray_where = null;
if(!empty($where)) {
$table_val_where = ":" . implode(',:', array_keys($where));
// Create an array of new keys that contain : in front of them
$table_key_where = explode(",", $table_val_where);
// get array values
$values_where = array_values($where);
// combine key with their respective values
$paramsArray_where = array_combine($table_key_where, $values_where);
$args = array();
foreach($where as $key=>$value)
$args[] = "$key=:$key";
$sql .= " WHERE " . implode(' && ', $args);
}
$this->stmt = $this->pdo->prepare($sql);
if($this->stmt->execute($paramsArray_where))
return true;
}
return false;
}
private function tableExists($table) {
$sql = "SHOW TABLES FROM " . $this->db_name . " LIKE '$table'";
$this->stmt = $this->pdo->query($sql);
if($this->stmt->execute()) {
if($this->stmt->rowCount() > 0)
return true;
}
return false;
}
public function getResult() {
$result = $this->result;
$this->result = array();
return $result;
}
public function __destruct()
{
if($this->conn) {
$this->conn = false;
$this->pdo = null;
$this->dsn = "";
$this->stmt = null;
$this->result = array();
return true;
}else{
return false;
}
}
}
I have tested the above code and it works fine.
return
stackoverflow.com/q/6849572/2943403 Okay I see a lot of things to refine. I'll wait to see if this question gets closed before I start an answer. \$\endgroup\$$join_side
meant to hold? There is too much to critique with the little time I have. I'll defer to other contributors. \$\endgroup\$