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');
database.class.php:
class Database {
private $dbhost = DB_HOST;
private $dbuser = DB_USER;
private $dbpass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct() {
// set DSN
$dsn = 'mysql:host=' . $this->dbhost . ';dbname=' . $this->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, $this->dbuser, $this->dbpass, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
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);
}
}
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 '13 at 2:56