Skip to content

Instantly share code, notes, and snippets.

@n0m4dz
Last active September 29, 2022 11:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save n0m4dz/6b0ae1f02c71c168cf46 to your computer and use it in GitHub Desktop.
Save n0m4dz/6b0ae1f02c71c168cf46 to your computer and use it in GitHub Desktop.
PHP PDO database CRUD class
<?php
class Database extends PDO
{
public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
{
parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
}
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC)
{
$sth = $this->prepare($sql);
foreach ($array as $key => $value) {
$sth->bindValue("$key", $value);
}
if(!$sth->execute()){
$this->handleError();
}
else{
return $sth->fetchAll($fetchMode);
}
}
public function insert($table, $data)
{
ksort($data);
$fieldNames = implode('`, `', array_keys($data));
$fieldValues = ':' . implode(', :', array_keys($data));
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}
if(!$sth->execute()){
$this->handleError();
//print_r($sth->errorInfo());
}
}
public function update($table, $data, $where)
{
ksort($data);
$fieldDetails = NULL;
foreach($data as $key=> $value) {
$fieldDetails .= "`$key`=:$key,";
}
$fieldDetails = rtrim($fieldDetails, ',');
$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}
$sth->execute();
}
public function delete($table, $where, $limit = 1)
{
return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit");
}
/* count rows*/
public function rowsCount($table){
$sth = $this->prepare("SELECT * FROM ".$table);
$sth->execute();
return $sth -> rowCount();
}
/* error check */
private function handleError()
{
if ($this->errorCode() != '00000')
{
if ($this->_errorLog == true)
//Log::write($this->_errorLog, "Error: " . implode(',', $this->errorInfo()));
echo json_encode($this->errorInfo());
throw new Exception("Error: " . implode(',', $this->errorInfo()));
}
}
}
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'my_db');
define('DB_USER', 'root');
define('DB_PASS', 'password');
$this->db = new Database(DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS);
@abba-code
Copy link

Thank you very much. you did a great work though i can not access the usage.php file

@masoud2518
Copy link

Thank you

@shone78
Copy link

shone78 commented Aug 30, 2021

Can you post examples of Select, Insert, Update and Delete in Usage.php?
Thanks

@iyikodcom
Copy link

iyikodcom commented Feb 23, 2022

Thank you, how can i terminate the database connection ?

@serial
Copy link

serial commented Sep 22, 2022

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

See documentation php.net/manual/en/pdo.connections.php - Example #3 Closing a connection

@tfanshool
Copy link

Can't we disconnect the connection in the destructor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment