Skip to content

Instantly share code, notes, and snippets.

@jawdatls
Created May 18, 2015 11:33
Show Gist options
  • Select an option

  • Save jawdatls/4625025992423ba0b0e9 to your computer and use it in GitHub Desktop.

Select an option

Save jawdatls/4625025992423ba0b0e9 to your computer and use it in GitHub Desktop.
DB Mysql Class helper
<?php
// db config
if(!defined('DB_SERVER')){ define('DB_SERVER','localhost'); }
if(!defined('DB_NAME')){ define('DB_NAME','db_name'); }
if(!defined('DB_USERNAME')){ define('DB_USERNAME','root'); }
if(!defined('DB_PASSWORD')){ define('DB_PASSWORD',''); }
if(!defined('DB_PREFIX')){ define('DB_PREFIX',''); }
class DB
{
public static function utf8(){
mysql_query('SET CHARACTER SET "utf8"');
}
public static function open(){
$res = mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
if($res){
self::utf8();
$res = mysql_select_db(DB_NAME);
}
return $res;
}
public static function close(){
$res = mysql_close();
return $res;
}
public static function stringval($value)
{
if(get_magic_quotes_gpc()){
$value = stripslashes($value);
} else {
$value = addslashes($value);
}
return $value;
}
public static function select($qr,$type = 'object')
{
$res = mysql_query($qr);
$result = (object) array();
if($res != null){
$count = mysql_num_rows($res);
if($count > 0){
switch($type){
case 'object':
$result = mysql_fetch_object($res);
break;
case 'array':
$result = mysql_fetch_array($res);
break;
}
}else{
$result = NULL;
}
}
return $result;
}
public static function selectAll($qr,$type = 'object')
{
$res = mysql_query($qr);
$count = mysql_num_rows($res);
$result = array();
if($count > 0){
switch($type){
case 'object':
while($r = mysql_fetch_object($res)){
$result[] = $r;
}
break;
case 'array':
while($r = mysql_fetch_array($res)){
$result[] = $r;
}
break;
}
}else{
$result = NULL;
}
return $result;
}
public static function insert($table , $data = array())
{
$res = false;
if(!empty($data) && is_array($data)){
$sql = 'INSERT INTO '.DB_PREFIX.$table.' ';
$columns = array();
$values = array();
foreach($data AS $k=>$v){
if(is_string($v)){
$v = self::stringval($v);
}
$columns[] = $k;
$values[] = '"'.$v.'"';
}
$columns = implode(',',$columns);
$values = implode(',',$values);
$sql.= '( '.$columns.' ) VALUES ( '.$values.' )';
self::utf8();
$res = mysql_query($sql);
if($res){
$res = mysql_insert_id();
}
}
return $res;
}
public static function update($table , $data = array() , $where = ''){
$res = false;
if(!empty($data) && is_array($data)){
$sql = 'UPDATE '.DB_PREFIX.$table.' SET ';
$d = array();
$w = array();
foreach($data AS $k => $v){
if(is_string($v)){
$v = self::stringval($v);
}
$d[] = $k.' = "'.$v.'" ';
}
$sql.= implode(',',$d);
if(isset($where) && !empty($where)){
$sql.= ' WHERE '.$where;
}
self::utf8();
$res = mysql_query($sql);
}
return $res;
}
public static function delete($table , $where = ''){
$sql = ' DELETE FROM '.DB_PREFIX.$table;
if($where != ''){
$sql.= ' WHERE '.$where;
}
$res = mysql_query($sql);
return $res;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment