Skip to content

Instantly share code, notes, and snippets.

@ennjoy
Created June 18, 2019 16:54
Show Gist options
  • Save ennjoy/992cf2923a8d7afe6f67a902cbc82bc3 to your computer and use it in GitHub Desktop.
Save ennjoy/992cf2923a8d7afe6f67a902cbc82bc3 to your computer and use it in GitHub Desktop.
class SQL.php
<?php
class SQL {
private $where;
private $select = '*';
private $limit;
private $order_by;
private $query;
private $set;
private function query_string($table) {
$query = ($this->query) ? $this->query : "SELECT $this->select FROM $table WHERE $this->where $this->order_by $this->limit";
return $query;
}
function query($text) {
$this->query = $text;
return $this;
}
function where_text($text) {
$operator = 'AND';
if (!$this->where) {
$this->where = $text;
} else {
$this->where .= $operator." ".$text;
}
return $this;
}
function order_by_text($text) {
if (!$this->order_by){
$this->order_by = ' ORDER BY '.$text;
} else {
$this->order_by .= ', '.$text;
}
}
function connect($host, $user, $pass, $name) {
$db = @mysql_connect($host, $user, $pass);
mysql_set_charset('utf8',$db);
if (!$db) exit("<p>Sorry, not available MySQL server</p>");
if (!@mysql_select_db($name, $db)) exit("<p>Unfortunately, the database is not available</p>");
}
function select($column) {
$this->select = $column;
return $this;
}
function limit($start,$count) {
$this->limit = ' LIMIT '.$start.','.$count;
return $this;
}
function order_by($key) {
if (is_array($key)) {
foreach ($key as $column => $value) {
if (!$this->order_by){
$this->order_by = ' ORDER BY '.$column.' '.$value;
} else {
$this->order_by .= ', '.$column.' '.$value;
}
}
} else {
if (!$this->order_by){
$this->order_by = ' ORDER BY '.$key;
} else {
$this->order_by .= ', '.$key;
}
}
return $this;
}
function where($key,$compare = '=') {
$operator = 'AND';
if (is_array($key)){
foreach ($key as $column=>$value){
if (!$this->where){
$this->where = $column.$compare."'".$value."' ";
} else {
$this->where .= $operator." ".$column.$compare."'".$value."' ";
}
}
} else {
$key = explode(',',$key);
if (!$this->where){
$this->where = $key[0].$compare."'".$key[1]."' ";
} else {
$this->where .= $operator." ".$key[0].$compare."'".$key[1]."' ";
}
}
return $this;
}
function search($column,$search){
$operator = 'AND';
$search = strtr($search,array(" "=>" +"));
$search = '+'.$search;
$w = 'MATCH('.$column.") AGAINST('".$search."' IN BOOLEAN MODE)";
if (!$this->where){
$this->where = $w;
} else {
$this->where .= $operator." ".$w;
}
return $this;
}
function lines($table,$ext = true){
$msql_query = mysql_query($this->query_string($table));
$line = mysql_fetch_assoc($msql_query);
if ($ext)$this->exit_function();
return $line;
}
function multiline($table,$ext = true){
$msql_query = mysql_query($this->query_string($table));
$multiline = array();
while($arr = mysql_fetch_assoc($msql_query)) $multiline[] = $arr;
if ($ext)$this->exit_function();
return $multiline;
}
function num_rows($table,$ext = true){
$msql_query = mysql_query($this->query_string($table));
$num_rows = mysql_num_rows($msql_query);
if ($ext)$this->exit_function();
return $num_rows;
}
function insert($table){
$query = 'INSERT '.$table.$this->set;
$q = mysql_query($query);
$this->exit_function();
return $q ? mysql_insert_id() : 'ERROR INSERT MYSQL';
}
function update($table) {
$query = 'UPDATE '.$table.$this->set.' WHERE '.$this->where;
$this->exit_function();
return mysql_query($query);
}
function set($value) {
$operator = ',';
if (is_array($value)){
foreach ($value as $column=>$val){
if (!$this->set){
$this->set = ' SET '.$column.'='."'".$val."' ";
} else {
$this->set .= $operator." ".$column.'='."'".$val."' ";
}
}
} else {
$value = explode(',',$value);
if (!$this->set){
$this->set = ' SET '.$value[0].'='."'".$value[1]."' ";
} else {
$this->where .= $operator." ".$value[0].'='."'".$value[1]."' ";
}
}
return $this;
}
private function exit_function(){
$this->where = '';
$this->select = '*';
$this->limit = '';
$this->order_by = '';
$this->query = '';
return $this;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment