Created
May 18, 2015 11:33
-
-
Save jawdatls/4625025992423ba0b0e9 to your computer and use it in GitHub Desktop.
DB Mysql Class helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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