setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con = true; } catch (PDOException $e) { self::$customError = $e->getMessage(); self::$con = false; } } } public static function isConnected() { return self::$con; } public static function select($table, $columns = '*', $where = NULL, $limit = NULL) { $sql = "SELECT $columns FROM $table"; $params = array(); if($where != NULL) { if(!is_array($where)) { echo 'Error in DB::select function: \'where\' parameter must be Array'; return false; } if(!is_array($where[0]) && count($where) == 2) { $where = array($where); } $sql .= " WHERE"; for($i = 0; $i < count($where); $i++) { $w = $where[$i]; if(is_array($w)) { if(count($w) == 2) { $sql .= " `$w[0]` = ?"; $params[] = $w[1]; } else if(count($w) == 3) { $sql .= " `$w[0]` $w[1] = ?"; $params[] = $w[2]; } if($i != count($where)-1) { $sql = " AND"; } } } } if($limit != NULL) { $sql = " LIMIT $limit"; } return self::query($sql, $params); } public static function query($query, $params = array()) { try { $params = !is_array($params) ? array($params) : $params; $data = array(); $is_select = strtoupper(substr($query,0,6)) == "SELECT" ? true : false; if(count($params) > 0) { $stmt = self::$instance->prepare($query); if(!$stmt) { return false; } $stmt->execute($params); if($is_select) $data = $stmt->fetchAll(PDO::FETCH_ASSOC); else { $data = true; } $stmt->closeCursor(); } else { $res = self::$instance->query($query); if(!$res) { return false; } else if($is_select) { foreach($res as $row) $data[] = $row; } else { $data = true; } } self::$customError = ""; return $data; } catch(Exception $e) { self::setError($e->getMessage()); return false; } } public static function getError() { if(strlen(self::$customError) > 0) { return self::$customError; } else { $err = self::$instance->errorInfo(); return $err[count($err)-1]; } } public static function setError($e) { self::$customError = $e; } } // shit class class DB2 { private static $i; private static $con; private static $customError; public static function init($host, $user, $pass, $db) { if(!isset(self::$i)) { self::$i = new mysqli($host, $user, $pass, $db); self::$con = !(self::$i->connect_errno > 0); self::$customError = ""; } } public static function isConnected() { return self::$con; } public static function ConnectionError() { return self::$i->connect_error; } public static function tableExists($tbl_name) { $q = self::query("SHOW TABLES LIKE ?", $tbl_name); return !!$q && count($q) > 0; } public static function rawQuery($q) { return self::$i->query($q); } private static function getTypes($params) { $param_type = ''; foreach($params as $p) { switch(gettype($p)) { case 'blob': $param_type .= 'b'; break; case 'double': $param_type .= 'd'; break; case 'integer': $param_type .= 'i'; break; case 'NULL': case 'string': $param_type .= 's'; break; default: $param_type .= 's'; break; } } return $param_type; } public static function query($sql, $params = null) { $types = self::getTypes($params); # create a prepared statement $stmt = self::$i->prepared($sql); # bind parameters for markers # but this is not dynamic enough... //$stmt->bind_param("s", $parameter); if($types&&$params) { $bind_names[] = $types; for ($i=0; $iexecute(); # these lines of code below return one dimentional array, similar to mysqli::fetch_assoc() $meta = $stmt->result_metadata(); while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $parameters[$field->name] = &$$var; } call_user_func_array(array($stmt, 'bind_result'), $parameters); while($stmt->fetch()) { return $parameters; //print_r($parameters); } # the commented lines below will return values but not arrays # bind result variables //$stmt->bind_result($id); # fetch value //$stmt->fetch(); # return the value //return $id; # close statement $stmt->close(); } public static function query3($query, $a_bind_params = array()) { $is_select = strtoupper(substr($query,0,6)) == "SELECT" ? true : false; if($is_select && count($a_bind_params) == 0) { $res = self::$i->query($query); $a_data = array(); while($row = $res->fetch_assoc()) { $a_data[] = $row; } return $a_data; } else { if(count($a_bind_params) > 0) { $a_params = array(); $param_type = ''; for($i = 0; $i < count($a_bind_params); $i++) { switch(gettype($a_bind_params[$i])) { case 'blob': $param_type .= 'b'; break; case 'double': $param_type .= 'd'; break; case 'integer': $param_type .= 'i'; break; case 'NULL': case 'string': $param_type .= 's'; break; default: $param_type .= 's'; break; } } $param_type[] = &$param_type; echo "QUERY: ".$query."
"; echo "TYPES:
"; prettyprint($param_type); echo "
"; echo "PARAMS (".count($a_bind_params)."):
"; prettyprint($a_bind_params); for($i = 0; $i < count($a_bind_params);$i++) { $a_params[] = $a_bind_params[$i]; } } $stmt = self::$i->prepare($query); if($stmt == false) { return false; } call_user_func_array(array($stmt, 'bind_param'), $a_params); $stmt->execute(); if($is_select) { $a_data = array(); $res = $stmt->get_result(); while($row = $res->fetch_array(MYSQLI_ASSOC)) { $a_data[] = $row; } return $a_data; } else { return true;; } } } public static function query2($query, $params = array()) { $data = array(); $is_select = strtoupper(substr($query,0,6)) == "SELECT" ? true : false; if(!is_array($params)) $params = array($params); $stmt = self::$i->prepare($query); if(!$stmt) return false; $types = ""; foreach($params as $value) { switch(gettype($value)) { case 'blob': $types .= 'b'; break; case 'double': $types .= 'd'; break; case 'integer': $types .= 'i'; break; case 'NULL': case 'string': $types .= 's'; break; default: $types .= 's'; break; } } echo str_repeat("-",8)."
"; echo "Query: ".$query."
"; echo "Types(".strlen($types)."): ".$types."
"; echo "Params(".count($params)."): "; prettyprint($params); echo str_repeat("-",8)."
"; $stmt->bind_param($types, $params); $stmt->execute(); if($is_select) { $res = $stmt->get_result(); $stmt->close(); if(!$res) return false; while($row = $res->fetch_array(MYSQLI_ASSOC) ) { $data[] = $row; } self::$customError = ""; return $data; } else { return true; } } public static function getError() { return strlen(self::$customError) > 0 ? self::$customError : self::$i->error; } public static function setError($e) { self::$customError = $e; } public static function escape($v) { return self::$i->real_escape_string($v); } }