Skip to content

Instantly share code, notes, and snippets.

@Terrance
Created September 30, 2014 15:11
Show Gist options
  • Save Terrance/a5aea5deb5d923578122 to your computer and use it in GitHub Desktop.
Save Terrance/a5aea5deb5d923578122 to your computer and use it in GitHub Desktop.
A simplified PHP wrapper for accessing a MySQL database, including debugging using Debug.php.
<?
include "Debug.php";
class DB {
private static $conn = null;
private static $prefix = "";
public static function connect($host="localhost", $user="root", $pass=null, $db=null) {
DB::$conn = mysqli_connect($host, $user, $pass, $db);
}
public static function setPrefix($newPrefix="") {
DB::$prefix = ($newPrefix ? $newPrefix : "");
}
public static function query($query) {
Debug::post("Query sent to database.\n" . $query, Debug::info);
return mysqli_query(DB::$conn, $query);
}
public static function error() {
return mysqli_error(DB::$conn);
}
public static function insert($table, $vars) {
if (!DB::$conn) {
DB::connect();
}
$rows = array_keys($vars);
$vals = array_values($vars);
foreach ($vals as $i => $val) {
switch (gettype($val)) {
case "integer":
case "double":
$vals[$i] = (string) $val;
break;
case "boolean":
$vals[$i] = ($val ? "1" : "0");
break;
case "string":
$vals[$i] = "\"" . mysqli_real_escape_string(DB::$conn, $val) . "\"";
break;
}
}
$table = DB::$prefix . $table;
Debug::post("Database insert query.\nTable | " . $table . "\nRows&nbsp; | " . implode(", ", $rows) . "\nVals&nbsp; | " . implode(", ", $vals));
$result = DB::query("INSERT INTO `" . $table . "` (`" . implode("`, `", $rows) . "`) VALUES (" . implode(", ", $vals) . ");");
if ($result) {
Debug::post("Added successfully.", Debug::success);
return True;
} else {
Debug::post("Database error occured.\n" . DB::error(), Debug::error);
return False;
}
}
public static function update($table, $where, $vars) {
if (!DB::$conn) {
DB::connect();
}
$rows = array_keys($vars);
$vals = array_values($vars);
$pairs = array();
foreach ($vals as $i => $val) {
switch (gettype($val)) {
case "integer":
case "double":
$vals[$i] = (string) $val;
break;
case "boolean":
$vals[$i] = ($val ? "1" : "0");
break;
case "string":
$vals[$i] = "\"" . mysqli_real_escape_string(DB::$conn, $val) . "\"";
break;
}
$pairs[] = "`" . $rows[$i] . "` = " . $vals[$i];
}
$table = DB::$prefix . $table;
Debug::post("Database update query.\nTable | " . $table . "\nWhere | " . ($where ? $where : "n/a") . "\nRows&nbsp; | " . implode(", ", $rows) . "\nVals&nbsp; | " . implode(", ", $vals));
$result = DB::query("UPDATE `" . $table . "` SET " . implode(", ", $pairs) . ($where ? " WHERE " . $where : "") . ";");
if ($result) {
Debug::post("Row(s) changed.", Debug::success);
return True;
} else {
Debug::post("Database error occured.\n" . DB::error(), Debug::error);
return False;
}
}
public static function delete($table, $where) {
if (!DB::$conn) {
DB::connect();
}
$table = DB::$prefix . $table;
Debug::post("Database delete query.\nTable | " . $table . "\nWhere | " . ($where ? $where : "n/a"));
$result = DB::query("DELETE FROM `" . $table . "`" . ($where ? " WHERE " . $where : "") . ";");
if ($result) {
Debug::post("Row(s) deleted.", Debug::success);
return True;
} else {
Debug::post("Database error occured.\n" . DB::error(), Debug::error);
return False;
}
}
public static function fetch($table, $where=null, $sort=null, $limit=array(0, 100)) {
if (!DB::$conn) {
DB::connect();
}
$table = DB::$prefix . $table;
Debug::post("Database select query.\nTable | " . $table . "\nWhere | " . ($where ? $where : "n/a") . "\nSort&nbsp; | " . ($sort ? $sort : "n/a") . "\nLimit | " . implode(" -> ", $limit));
$conds = "";
if ($where) {
$conds .= " WHERE " . $where;
}
if ($sort) {
$conds .= " ORDER BY " . $sort;
}
$conds .= " LIMIT " . implode(", ", $limit);
$result = DB::query("SELECT * FROM `" . $table . "`" . $conds . ";");
$data = array();
if ($result) {
$count = 0;
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
$count++;
}
Debug::post($count . " row(s) returned.", Debug::success);
} else {
Debug::post("No data returned.", Debug::warning);
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment