i created a database class from a good tutorial and wanted to put it up here so it would get in some search results. it took me about 2 days to find it. also i added a few custom functions to it.. here it is :P and if there is something that can be done better or more proficiently please feel free to let me know.
config.php:
// Database Constants
defined('DB_HOST') ? NULL : define('DB_HOST', 'edit:host');
defined('DB_USER') ? NULL : define('DB_USER', 'edit:user');
defined('DB_PASS') ? NULL : define('DB_PASS', 'edit:pass');
defined('DB_NAME') ? NULL : define('DB_NAME', 'edit:databasename');
edited above to:
if(!defined( 'DB_HOST' )) define( 'DB_HOST', 'localhost' );
if(!defined( 'DB_USER' )) define( 'DB_USER', '68_dev' );
if(!defined( 'DB_PASS' )) define( 'DB_PASS', 'PZ8Jv8p7aqCJwmSy' );
if(!defined( 'DB_NAME' )) define( 'DB_NAME', '68_dev' );
Database.class.php
public function __construct($dbhost = DB_HOST,$dbuser = DB_USER,
$dbpass = DB_PASS,$dbname = DB_NAME ) {
// set DSN
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname;
// set OPTIONS
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instance
try {
$this->dbh = new PDO($dsn, $dbuser, $dbpass, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
private function buildHolder($array) {
for ($i = 0; $i < count($array); $i++) {
$array[$i] = ':' . $array[$i];
}
return $array;
}
private function buildQuery($table,$field,$holder,$id) {
$query = 'UPDATE ' . $table . ' SET ';
$setStmt[] = NULL;
for($i = 0;$i < count($field);$i++){
$setStmt[$i]= $field[$i] . '=' . $holder[$i];
}
$query .= implode(',', $setStmt) . ' WHERE id=' . $id;
return $query;
}
start custom methods
public function updateQuery($table, $field, $value, $id) {
try {
if (( gettype($field) != 'array' ) || ( gettype($value) != 'array' )) {
$field = (array) $field;
$value = (array) $value;
}
$holder = $this->buildHolder($field);
$array = array_combine($holder, $value);
if(count($field) > 1){
$query = $this->buildQuery($table,$field,$holder,$id);
} else {
$query = 'UPDATE ' . $table . ' SET ' . implode(',', $field) . '=' . implode(',', $holder) .
' WHERE id=' . $id;
}
$this->query($query);
$this->bindArray($array);
$this->execute();
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function selectQuery($table, $fields, $FieldToQuery, $value) {
try {
if ((gettype($fields) != 'array') || (gettype($value) != 'array')) {
$fields = (array) $fields;
$FieldToQuery = (array) $FieldToQuery;
$value = (array) $value;
}
$holders = $FieldToQuery;
for ($i = 0; $i < count($holders); $i++) {
$holders[$i] = ':' . $holders[$i];
}
$array = array_combine($holders, $value);
$query = 'SELECT ' . implode(',', $fields) . ' FROM ' . $table . ' WHERE ' .
implode(',',$FieldToQuery) . ' = ' . implode(',', $holders);
$this->query($query);
$this->bindArray($array);
$rows = $this->resultset();
return $rows;
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function insertQuery($table, $fields, $values) {
try {
if ((gettype($fields) != 'array') || (gettype($values) != 'array')) {
$fields = (array) $fields;
$values = (array) $values;
}
$holders = $fields;
for ($i = 0; $i < count($holders); $i++) {
$holders[$i] = ':' . $holders[$i];
}
$array = array_combine($holders, $values);
$query = 'INSERT INTO ' . $table . '(' . implode(',', $fields)
. ') VALUES (' . implode(',', $holders) . ')';
$this->query($query);
$this->bindArray($array);
$this->execute();
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function bindArray($array) {
foreach ($array as $key => $value) {
$this->bind($key, $value);
}
}
end of custom methods
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
$this->stmt->execute();
}
public function resultset() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function rowCount() {
return $this->stmt->rowCount();
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function beginTransaction() {
return $this->dbh->beginTransaction();
}
public function endTransaction() {
return $this->dbh->commit();
}
public function cancelTransaction() {
return $this->dbh->rollBack();
}
public function debugDumpParams() {
return $this->stmt->debugDumpParams();
}
}
here is the link http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/
bindArray
,selectQuery
, andinsertQuery
methods look original. – cHao Feb 14 at 2:56