Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- public static $db;
- public static $results = array();
- public static $con = false;
- //Initializes database connection
- public static function init($server, $uid, $pwd, $db) {
- if(!isset(self::$db)) {
- //Connect to database
- self::$db = @new mysqli($server, $uid, $pwd, $db);
- //Check connection
- if(self::$db->connect_errno > 0) {
- self::UpdateResult(self::$db->connect_error, false);
- return false;
- }
- else {
- self::$con = true;
- }
- }
- return true;
- }
- //Returns escaped values
- public static function escape($value) {
- return self::$db->real_escape_string($value);
- }
- //Returns true or false
- public static function simple_query($q) {
- $q = @self::$db->query($q) ? true : false;
- if($q) {
- return true;
- }
- else {
- self::UpdateResult(self::$db->error, false);
- return false;
- }
- }
- //Returns the query object
- public static function query($q) {
- $q = @self::$db->query($q);
- if(!$q) {
- self::UpdateResult(self::$db->error, false);
- }
- return $q;
- }
- //Select statement
- public static function select($table, $rows = '*', $where = null, $order = null) {
- $q = "SELECT $rows FROM $table";
- if($where != null) {
- $q .= " WHERE ".$where;
- }
- if($order != null) {
- $q .= " ORDER BY ".$order;
- }
- echo $q."<br>";
- //Check if table exists
- if(self::tableExists($table)) {
- $query = self::query($q);
- if($query) {
- //Clear results
- self::$results = array();
- //Add results
- while($r = $query->fetch_assoc()) {
- self::UpdateResult($r);
- }
- return true;
- }
- else {
- self::UpdateResult(self::$db->error, false);
- }
- }
- return false;
- }
- public static function insert($table, $rows = null, $params) {
- $q = "INSERT INTO $table";
- if($rows != null) {
- if(!is_array($rows)) {
- $rows = preg_split("/,/", $rows);
- }
- $q .= " (".implode(", ", $rows).")";
- }
- $q .= " VALUES(";
- if(!is_array($params))
- $params = preg_split("/,/", $params);
- for($i = 0; $i < count($params); $i++) {
- $param = $params[$i];
- if(is_int($param)) {
- $q .= $param;
- }
- else {
- $q .= "'".$param."'";
- }
- if($i != count($params) - 1)
- $q .= ", ";
- }
- $q .= ");";
- return self::simple_query($q);
- }
- public static function update($table, $set, $where = null) {
- $q = "UPDATE $table SET";
- //If set is an array
- if(is_array($set)) {
- //If set is multiple arrays
- if(is_array($set[0])) {
- for($i = 0; $i < count($set); $i++) {
- $row = $set[$i][0];
- $value = $set[$i][1];
- $q .= " ".$row." = ";
- if(is_int($value)) {
- $q .= $value;
- }
- else {
- $q .= "'$value'";
- }
- if($i != count($set) - 1)
- $q .= ", ";
- }
- }
- //Single array
- else {
- $q .= " ".$set[0]." = ";
- if(is_int($set[1])) {
- $q .= $set[1];
- }
- else {
- $q .= "'".$set[1]."'";
- }
- }
- }
- else {
- if(!strstr($set, ",")) {
- $data = explode("=", $set);
- $q .= " ".$data[0]." = ";
- if(is_int($data[1])) {
- $q .= $data[1];
- }
- else {
- $q .= "'".$data[1]."'";
- }
- }
- else {
- $datas = explode(",", $set);
- for($i = 0; $i < count($datas); $i++) {
- $data = explode("=", $datas[$i]);
- $q .= " ".$data[0]." = ";
- if(is_int($data[1])) {
- $q .= $data[1];
- }
- else {
- $q .= "'".$data[1]."'";
- }
- if($i != count($datas) - 1)
- $q .= ", ";
- }
- }
- }
- if($where != null) {
- $q .= " WHERE ";
- if(!is_array($where) && strstr($where, ","))
- $where = preg_split("/,/", $where);
- if(is_array($where)) {
- $q .= implode(" AND ", $where);
- }
- else {
- $q .= $where;
- }
- }
- echo $q."<br>";
- return self::simple_query($q);
- }
- public static function delete($table, $where = null) {
- if($where != null) {
- $q = "DELETE FROM $table WHERE ".$where;
- }
- else {
- $q = "DROP TABLE $table";
- }
- echo $q."<br>";
- return self::simple_query($q);
- }
- public static function createTable($name, $data) {
- if(self::tableExists($name))
- return true;
- $q = "CREATE TABLE $name (";
- if(!is_array($data)) {
- $data = preg_split("/,/", $data);
- }
- for($i = 0; $i < count($data); $i++) {
- $q .= implode(" ", explode("=", $data[$i]));
- if($i != count($data) - 1)
- $q .= ", ";
- }
- $q .= ");";
- return self::simple_query($q);
- }
- public static function emptyTable($name) {
- return self::simple_query("TRUNCATE $name");
- }
- public static function tableExists($table) {
- $q = @self::query("SHOW TABLES LIKE '$table'");
- if($q) {
- if($q->num_rows > 0)
- return true;
- else
- return false;
- } else {
- return false;
- }
- }
- private static function UpdateResult($e, $append = true) {
- if($append)
- array_push(self::$results, $e);
- else
- self::$results = $e;
- }
- public static function getResults() {
- //Save result
- $v = self::$results;
- echo "Count: ".count($v)."<br>";
- //Clear result
- self::$results = array();
- //Return saved result
- return $v;
- //return count($v) == 1 ? $v[0] : $v;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement