Last active
March 25, 2020 06:16
-
-
Save surajitbasak109/1b9a1f51e8fdb04be20b7d6abd4acc07 to your computer and use it in GitHub Desktop.
Static DB Connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config['database'] = [ | |
'dbname' => 'data', | |
'dbhost' => 'localhost', | |
'dbdriver' => 'mysql', | |
'username' => 'root', | |
'password' => 'password' | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'config.php'; | |
class DB | |
{ | |
public static function connect() | |
{ | |
global $config; | |
extract($config['database']); | |
$options = [ | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
PDO::ATTR_EMULATE_PREPARES => false, | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
]; | |
$pdo = new PDO("{$dbdriver}:host={$dbhost};dbname={$dbname};", $username, $password, $options); | |
return $pdo; | |
} | |
/** | |
* | |
* @param string $table | |
* @param string $cols (optional) | |
* @param string $opts (optional) | |
* @return unknown | |
*/ | |
public static function select(string $table, string $cols = "*", string $opts = "") { | |
$stmt = self::connect()->prepare("SELECT {$cols} FROM {$table} {$opts}"); | |
$stmt->execute(); | |
return $stmt; | |
} | |
/** | |
* | |
* @param string $table | |
* @param string $cols (optional) | |
* @param array $where (optional) | |
* @param string $optional (optional) | |
* @return unknown | |
*/ | |
public static function select_where(string $table, string $cols = "*", array $where = [], string $optional = '') { | |
$whereCommand = ""; | |
foreach ($where as $key => $value) { | |
$whereCommand .= $key . " = " . "? AND "; | |
} | |
$whereCommand = rtrim($whereCommand, 'AND '); | |
$sql = "SELECT {$cols} FROM $table WHERE {$whereCommand} {$optional}"; | |
$stmt = self::connect()->prepare($sql); | |
$stmt->execute(array_values($where)); | |
return $stmt; | |
} | |
/** | |
* | |
* @param string $table | |
* @param array $cols | |
* @return unknown | |
*/ | |
public static function insert(string $table, array $cols) { | |
$cols_string = implode(", ", array_keys($cols)); | |
$value_string = implode(", ", array_fill(0, count(array_keys($cols)), '?')); | |
$sql = "INSERT INTO $table ({$cols_string}) VALUES ({$value_string});"; | |
$stmt = self::connect()->prepare($sql); | |
try { | |
$stmt->execute(array_values($cols)); | |
} catch (Exception $e) { | |
return $e; | |
} | |
} | |
/** | |
* | |
* @param string $table | |
* @param array $data (optional) | |
* @return unknown | |
*/ | |
public static function batch_insert(string $table, array $data = []) { | |
//Will contain SQL snippets. | |
$rowsSQL = array(); | |
//Will contain the values that we need to bind. | |
$toBind = array(); | |
//Get a list of column names to use in the SQL statement. | |
$columnNames = array_keys($data[0]); | |
//Loop through our $data array. | |
foreach ($data as $arrayIndex => $row) { | |
$params = array(); | |
foreach ($row as $columnName => $columnValue) { | |
$param = ":" . $columnName . $arrayIndex; | |
$params[] = $param; | |
$toBind[$param] = $columnValue; | |
} | |
$rowsSQL[] = "(" . implode(", ", $params) . ")"; | |
} | |
//Construct our SQL statement | |
$sql = "INSERT INTO `$table` (" . implode(", ", $columnNames) . ") VALUES " . implode(", ", $rowsSQL); | |
//Prepare our PDO statement. | |
$stmt = self::connect()->prepare($sql); | |
//Bind our values. | |
foreach ($toBind as $param => $val) { | |
$stmt->bindValue($param, $val); | |
} | |
//Execute our statement (i.e. insert the data). | |
return $stmt->execute(); | |
} | |
/** | |
* | |
* @param string $table | |
* @param array $colname (optional) | |
* @param array $data (optional) | |
* @return unknown | |
*/ | |
public static function update(string $table, array $colname = [], array $data = []) { | |
$fieldname = implode(',', array_keys($colname)); | |
$sqlCommand = ""; | |
foreach ($data as $key => $value) { | |
$sqlCommand .= $key . '=' . '?, '; | |
} | |
$sqlCommand = rtrim($sqlCommand, ', '); | |
$sql = "UPDATE {$table} SET {$sqlCommand} WHERE {$fieldname} = ?"; | |
$stmt = self::connect()->prepare($sql); | |
$params = array_merge(array_values($data), array_values($colname)); | |
return $stmt->execute($params); | |
} | |
/** | |
* | |
* @param string $table | |
* @param array $colname (optional) | |
* @return unknown | |
*/ | |
public static function delete(string $table, array $colname = []) { | |
$fieldname = implode(',', array_keys($colname)); | |
$sql = "DELETE FROM {$table} WHERE {$fieldname} = ?"; | |
$stmt = self::connect()->prepare($sql); | |
return $stmt->execute(array_values($colname)); | |
} | |
/** | |
* batch_update | |
* | |
* | |
* @param string $table | |
* @param array $data (optional) | |
* @param array $colname (optional) | |
* @return void | |
*/ | |
public function batch_update(string $table, array $data = [], array $colname = []) { | |
if ($this->delete($table, $colname)) { | |
//Will contain SQL snippets. | |
$rowsSQL = array(); | |
//Will contain the values that we need to bind. | |
$toBind = array(); | |
//Get a list of column names to use in the SQL statement. | |
$columnNames = array_keys($data[0]); | |
//Loop through our $data array. | |
foreach ($data as $arrayIndex => $row) { | |
$params = array(); | |
foreach ($row as $columnName => $columnValue) { | |
$param = ":" . $columnName . $arrayIndex; | |
$params[] = $param; | |
$toBind[$param] = $columnValue; | |
} | |
$rowsSQL[] = "(" . implode(", ", $params) . ")"; | |
} | |
//Construct our SQL statement | |
$sql = "INSERT INTO `$table` (" . implode(", ", $columnNames) . ") VALUES " . implode(", ", $rowsSQL); | |
//Prepare our PDO statement. | |
$pdoStatement = self::connect()->prepare($sql); | |
//Bind our values. | |
foreach ($toBind as $param => $val) { | |
$pdoStatement->bindValue($param, $val); | |
} | |
//Execute our statement (i.e. insert the data). | |
return $pdoStatement->execute(); | |
} | |
return false; | |
} | |
} | |
DB::update('posts', ['id' => 2], ['title' => 'Updated title 2']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment