I followed this tutorial to create an SQL "factory".
The link shows how to make a class who's methods will output SQL statements based on the arguments you pass to it, and I've expanded on it with the class below.
Basically, as an example of its usage: if you wanted to select from a database you can pass it an array of attributes to select, followed by the table name, and if there are conditions to the select, you pass it an array, where the array key is the column the condition is based on, and the array value is the value of that column - so a simple select would look something like this:
$theAttributes = array('name','age','height') $theTable = 'people'; $theConditions = array('hair_colour' => 'brown'); $select = $this->executeSelect($theAttributes,$theTable,$theConditions);
and then $insert[0]
would be the number of rows that were selected by the query, and insert[1]
would be an array containing each row that was returned as an array.
I'm just wondering if I could get some feedback on it as it seems to work fine for me, but I'm very new to PHP, so some advice such as inefficiency, insecurity, or if it's just plain wrong.
require './config/config.inc.php'; require './config/n_spaces.inc.php'; require_once(__DIR__.'/SqlStatement.php'); require_once(__DIR__.'/MySqlStatement.php'); class PDOdriver { public $conn; public function __construct() { $db_host = DB_HOST; $dbname = DB_NAME; $this->conn = new PDO("mysql:host=$db_host;dbname=$dbname",DB_USER,DB_PASS) or die('Cannot connect to the server, please inform your Network Administrator'); $this->st = new MySqlStatement(); } public function executeInsert($theAttributes,$theTable,$theConditions = NULL) { foreach($theAttributes as $index => $value) { $bind_params[] = ":$index"; } if(!isset($theConditions)){ $sqlstmt = trim($this->st->setTables($theTable)->setAttributes(array_keys($theAttributes))->setValues($bind_params)->makeInsert()); } else { $sqlstmt = trim($this->st->setTables($theTable)->setAttributes(array_keys($theAttributes))->setValues($bind_params)->setConditions($theConditions)->makeInsert()); } if($stmt = $this->conn->prepare($sqlstmt)) { foreach($theAttributes as $index => &$value) { $stmt->bindParam(":$index",$value); } if($stmt->execute()) { $rowCount = $stmt->rowCount(); $success[0] = $rowCount; $success[1] = $this->conn->lastInsertId(); return $success; } else { $success[0] = 'Cannot Execute'; $stmt = NULL; return $success; } } else { $success[0] = 'Cannot Prepare'; $stmt = NULL; return $success; } } public function executeSelect($theAttributes,$theTable,$theConditions = NULL,$fetch_style = "FETCH_ASSOC") { if(is_array($theConditions)) { foreach($theConditions as $index => $value){ $bind_conditions[] = "$index = :$index"; } $sqlstmt = trim($this->st->setAttributes($theAttributes)->setTables($theTable)->setConditions($bind_conditions)->makeSelect()); } elseif(!is_null($theConditions) && !is_array($theConditions)){ $sqlstmt = trim($this->st->setAttributes($theAttributes)->setTables($theTable)->setConditions($theConditions)->makeSelect()); } else { $sqlstmt = trim($this->st->setAttributes($theAttributes)->setTables($theTable)->makeSelect()); } if($stmt = $this->conn->prepare($sqlstmt)) { if(is_array($theConditions)) { foreach($theConditions as $index => &$value) { $stmt->bindParam(":$index",$value); } } if($stmt->execute()) { $success[0] = $stmt->rowCount(); if($success[0] > 0) { if($fetch_style === "FETCH_ASSOC") { while($result = $stmt->fetch(PDO::FETCH_ASSOC)) { $results[] = $result; } $success[1] = $results; } elseif($fetch_style === "FETCH_BOTH"){ while($result = $stmt->fetch(PDO::FETCH_BOTH)) { $results[] = $result; } $success[1] = $results; } elseif($fetch_style === "COUNT"){ } } $stmt = NULL; return $success; } else { $success[0] = 'Cannot Execute'; $stmt = NULL; return $success; } } else { $success[0] = 'Cannot Prepare'; $stmt = NULL; return $success; } } public function executeUpdate($theAttributesColumn,$theAttributesValue,$theTable,$theConditions) { if(is_array($theConditions)) { foreach($theConditions as $index => $value){ $bind_conditions[] = "$index = :$index"; } $sqlstmt = trim($this->st->setUpdateAttributes($theAttributesColumn, $theAttributesValue)->setTables($theTable)->setConditions($bind_conditions)->makeUpdate()); } else { $sqlstmt = trim($this->st->setUpdateAttributes($theAttributesColumn, $theAttributesValue)->setTables($theTable)->makeUpdate()); } if($stmt = $this->conn->prepare($sqlstmt)) { if(is_array($theConditions)) { foreach($theConditions as $index => &$value) { $stmt->bindParam(":$index",$value); } } if($stmt->execute()) { $success[0] = $stmt->rowCount(); return $success; } else { $success[0] = 'Cannot Execute'; $stmt = NULL; return $success; } } else { $success[0] = 'Cannot Prepare'; $stmt = NULL; return $success; } } }