柳暗花明笔记

    两年前写的 php db class

    所属分类:[ php&web ] 猫哲学家 发表于:2012-07-25 15:08 浏览:613

    $_DBHOST = 'localhost'; $_DBUESERNAME = 'root'; $_DBPS = '123456'; $_SELECTDB = 'table'; $conn = mysql_connect($_DBHOST,$_DBUESERNAME,$_DBPS); mysql_select_db($_SELECTDB,$conn) or die('do not select db!'); class lmzdb{ //private $db = 'test'; public function insert_data($table_name,$data_array,$char='utf8',$returnid=0){//utf8 also add SQL !! $field = $fieldvalue = $comma = ''; foreach ($data_array as $insert_key => $insert_value){ $field .= $comma.$insert_key; $fieldvalue .= $comma.'\''.mysql_real_escape_string($insert_value).'\''; $comma = ','; } $mysql = 'insert into '.$table_name.' '.'('.$field.') values ('.$fieldvalue.')'; mysql_query('set names '.$char); mysql_query($mysql) or die(mysql_error()); if($returnid) { return mysql_insert_id(); } } public function is_field_exist_value($table_name,$field,$value){//return result rows $mysql = 'select '.$field.' from '.$table_name.' where '.$field.' = \''.$value.'\''; $mysql_result = mysql_query($mysql) or die(mysql_error()); //$query_rows_array = mysql_fetch_array($mysql_result); !!error return mysql_num_rows($mysql_result); } public function return_last_insert_id($table_name,$field = 'id' ){//return last insert id ? is max // select id from uchome_doing where uid = 6 order by doid desc limit 1 $mysql = 'select '.$field.' from '.$table_name.' order by '.$field.' desc limit 1'; $mysql_result = mysql_query($mysql) or die(mysql_error()); $row = mysql_num_rows($mysql_result); $last_id = 0; if($row){ $rows_array = mysql_fetch_array($mysql_result); $last_id = $rows_array[$field]; return $last_id; }else{ return 0; } } //select * from $table where id = 'sf' and title = ''; public function query_data($table_name,$sort_string=null,$comdition_array,$char='utf8',$order=''){ //return data_array() and rows also add SQL !! order by id desc / asc $result = array(); if(!empty($sort_string)){ $mysql = 'select '.$sort_string.' from '.$table_name; }else{ $mysql = 'select * from '.$table_name; } $tem1 = $tem2 = ''; $mysql_result = $mysql; if(!empty($comdition_array)){ $array_sizeof = sizeof($comdition_array); $i = 0; foreach($comdition_array as $key => $value){ $value = mysql_real_escape_string($value); // add SQL !! $tem1 .= $key.' = '; $i<$array_sizeof-1?$tem2 = '\''.$value.'\''.' and ':$tem2 = '\''.$value.'\''; $tem1 .= $tem2; $i++; } //$mysql_result .= ' where '.$tem1.' limit 10'; $mysql_result .= ' where '.$tem1; } if(!empty($order)) $mysql_result .= $order; mysql_query('set names '.$char); $result['data_array'] = mysql_query($mysql_result) or die(mysql_error()); $result['rows'] = mysql_num_rows($result['data_array']); //if rows==0 return 0 else return array() if($result['rows']<=0){ return 0; }else{ return $result; } } public function delete_data($table_name,$comdition_array,$char='utf8'){ //also add SQL !! $mysql = 'delete from '.$table_name.' where '; $tem1 = $tem2 = ''; $array_sizeof = sizeof($comdition_array); $i = 0; foreach($comdition_array as $key => $value){ $value = mysql_real_escape_string($value); // add SQL !! $tem1 .= $key.' = '; $i<$array_sizeof-1?$tem2 = '\''.$value.'\''.' and ':$tem2 = '\''.$value.'\''; $tem1 .= $tem2; $i++; } $mysql_result = $mysql.$tem1; mysql_query('set names '.$char); mysql_query($mysql_result) or die(mysql_error()); $retu = mysql_affected_rows(); return $retu; } public function update_data($table_name,$update_array,$comdition_array,$char='utf8'){ //also add SQL !! $mysql = 'update '.$table_name.' set '; $tem1 = $tem2 = ''; $array_sizeof = sizeof($update_array); $i = 0; foreach($update_array as $key => $value){ $value = mysql_real_escape_string($value); // add SQL !! $tem1 .= $key.' = '; $i<$array_sizeof-1?$tem2 = '\''.$value.'\''.',':$tem2 = '\''.$value.'\''; $tem1 .= $tem2; $i++; } $mysql_result = $mysql.$tem1; // update test1 set title = 'tile',body = 'asdgfsd' //add where... $tem3 = $tem4 = ''; $array_sizeof1 = sizeof($comdition_array); $i = 0; foreach($comdition_array as $key => $value){ $tem3 .= $key.' = '; $i<$array_sizeof1-1?$tem4 = '\''.$value.'\''.' and ':$tem4 = '\''.$value.'\''; $tem3 .= $tem4; $i++; } $mysql_result .= ' where '.$tem3; mysql_query('set names '.$char); mysql_query($mysql_result) or die(mysql_error()); $retu = mysql_affected_rows(); return $retu; } public function ip_record($first=0,$table_name='web_ip_record',$get_client_ip=0,$get_last_ip=0,$insert_local_place=null){//frist check have table $client_ip = ''; if(getenv('HTTP_CLIENT_IP')){//get ip start $client_ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR')){ $client_ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR')){ $client_ip = getenv('REMOTE_ADDR'); }else{ $client_ip = $_SERVER['REMOTE_ADDR']; }//get ip end if($get_client_ip){ return $client_ip; } if($first){ //get all tables //$tables_array = array(); //$pointer = mysql_list_tables($this->db); //while($trow = mysql_fetch_array($pointer)){ //$tables_array[] = $trow[0]; //} //create ip record table $create_table_sql = 'create table '.$table_name.'(id int(11) unsigned not null auto_increment primary key,ip varchar(200) not null,datetime datetime not null,place varchar(250) not null)'; mysql_query($create_table_sql) or die(mysql_error()); $data_array = array( 'ip' => $client_ip, 'datetime' => date('Y-m-d').' '.date('H:i:s',time()+0)//28800 ); $this->insert_data($table_name,$data_array); } $last_ip_sql = 'select * from '.$table_name.' order by id desc limit 1'; $arr = mysql_fetch_assoc(mysql_query($last_ip_sql)); if($get_last_ip){ return $arr['ip']; } if($arr['ip']!=$client_ip){ $data_array = array( 'ip' => $client_ip, 'datetime' => date('Y-m-d').' '.date('H:i:s',time()+0),//28800 'place' => $insert_local_place==null?null:$insert_local_place ); $this->insert_data($table_name,$data_array); } } } global $mydb; $mydb = new lmzdb();
    标签: php 
    上一编文章 UCH2.0认证机制 下一编文章 UCenter密码算法规则和生成方法
    最近文章
    发表评论