db_host = $db_host; $this->db_user = $db_user; $this->db_pwd = $db_pwd; $this->db_database = $db_database; //$this->conn = $conn; $this->coding = $coding; $this->def=$def; $this->SQLList = array(); //$this->connect(); } /*数据库连接*/ public function connect() { //if ($this->conn == "pconn") { //永久链接 //$this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd) or die('数据库连接出错!'); //} else { //即使链接 if(!$this->conn){ $this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd) or die('数据库连接出错!'); if (!@mysql_select_db($this->db_database, $this->conn)) { if ($this->show_error) { $this->show_error("数据库不可用:", $this->db_database); } } @mysql_query("SET NAMES $this->coding"); @mysql_query("SET character_set_connection=utf8,character_set_results=utf8,character_set_client=binary", $this->conn); } //} } /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/ public function query($sql) { //$sqlstatic['stime'] = microtime(true); $this->connect(); if ($sql == "") { $this->show_error("SQL语句错误:", "SQL查询语句为空"); } $this->sql = $sql; //file_put_contents(APP_PATH."file.txt",$this->sql.";\n",FILE_APPEND); //记录下所有 修改 删除SQL语句 位置不能错 否则会影响正常SQL执行 //截取SQL语句的前7个字符 $sqlprx = mb_substr(trim($this->sql),0,7); //以下语句代表 凡是涉及 company_statis 表进行update的语句都记录下 //sqlinfo 执行SQL time 执行时间 uri 执行该SQL的模块链接 可以通过uri定位具体模块 if(stripos($sqlprx,'update')!==false && stripos($this->sql,'company_statis')!==false){ mysql_query("INSERT INTO `phpyun_z_sqlinfo` SET `sqlinfo` ='".$this->escape_string($this->sql)."',`time`='".date('Y-m-d H:i:s')."',`uri`='".$_SERVER["REQUEST_URI"]."'",$this->conn); } $result = mysql_query($this->sql,$this->conn); $this->result = $result; return $this->result; } /** * 通用SQL 执行 便于多表查询语句 */ function DB_query_all($sql,$type='one'){ $query = $this->query($sql); if($type=='all'){ while($row=$this->fetch_array($query)){$return[]=$row;} }else{ $return=$this->fetch_array($query); } return $return; } /** * 通用单条查询$tablename,$where = 1, $select="*" */ function DB_select_once($tablename, $where = 1, $select = "*") { $cachename=$tablename.$where; if(!$return=$this->Memcache_set($cachename)){//获取是否存在memcache $SQL = "SELECT $select FROM " . $this->def . $tablename . " WHERE $where limit 1"; $query = $this->query($SQL); $return=$this->fetch_array($query); $this->Memcache_set($cachename,$return);//设置memcache } return $return; } function update_all($tablename, $value, $where = 1) { $cachename=$tablename.$where; if(!$return=$this->Memcache_set($cachename)){//获取是否存在memcache $SQL = "UPDATE `" . $this->def . $tablename . "` SET $value WHERE $where"; $query = $this->query($SQL); $return=$this->fetch_array($query); $this->Memcache_set($cachename,$return);//设置memcache } return $return; } function insert_once($tablename, $value) { $cachename=$tablename.$value; if(!$return=$this->Memcache_set($cachename)){//获取是否存在memcache $SQL = "INSERT INTO `" . $this->def . $tablename . "` SET ".$value; $this->query("set sql_mode=''"); $query = $this->query($SQL); $nid= $this->insert_id(); } return $nid; } function Memcache_set($name,$value=""){ $this->connect(); /** global $config; if($config[ismemcache]==2)return ""; $memcachehost=$config[memcachehost];//所在服务器 $memcacheport=$config[memcacheport];//所在服务器端口 $memcachezip=0;//是否支持解压缩 $memcachetime=$config[memcachetime];//缓存时间 $name=str_replace(array(" ","`","'",".","="),"",$name); $memcache = new memcache; $memcache->connect($memcachehost,$memcacheport) or die ("Memcache连接失败或您的服务器不支持Memcache,请在后台关闭!"); $val=$memcache->get($name); if(!is_array($val) && !$val){ $memcache->set($name,$value,$memcachezip,$memcachetime); } //$memcache->delete($name);//删除当前名称的cache //print_r($val); $memcache->close(); return $val; **/ } /** * 通用数量查询$tablename,$where = 1, $select="*" */ function select_num($tablename, $where = 1, $select = "*") { $cachename=$tablename.$where; if(!$return=$this->Memcache_set($cachename)){//获取是否存在memcache $SQL = "SELECT count($select) FROM " . $this->def . $tablename . " WHERE $where"; $query = $this->query($SQL); while($rs = mysql_fetch_array($query)){ $row = $rs; } $return=$row[0]; $this->Memcache_set($cachename,$return);//设置memcache } return $return; } /** * 通用all查询 $tablename,$where = 1, $select="*" */ function select_all($tablename, $where = 1, $select = "*") { $row_return=array(); $SQL = "SELECT $select FROM `" . $this->def . $tablename . "` WHERE $where"; $query=$this->query($SQL); while($row=$this->fetch_array($query)){$row_return[]=$row;} return $row_return; } /** * 通用$tablename为(select * from `test` order by `date` desc) `temp` * select * from (select * from `test` order by `date` desc) `temp` group by category_id order by `date` desc * 查询 $tablename, $where = 1, $select = "*" */ function select_only($tablename, $where = 1, $select = "*") { $row_return=array(); $SQL = "SELECT $select FROM " .$tablename . " WHERE $where"; $query=$this->query($SQL); while($row=$this->fetch_array_old($query)){$row_return[]=$row;} return $row_return; } /** * 通用all查询双表 $tablename1,$tablename2, $where = 1, $select = "*" */ function select_alls($tablename1,$tablename2, $where = 1, $select = "*") { $SQL = "SELECT $select FROM " . $this->def . $tablename1. " as a," . $this->def . $tablename2 . " as b WHERE $where"; $query=$this->query($SQL); while($row=$this->fetch_array($query)){$row_return[]=$row;} return $row_return; } /*创建添加新的数据库*/ public function create_database($database_name) { $database = $database_name; $sqlDatabase = 'create database ' . $database; $this->query($sqlDatabase); } function cacheget(){ include PLUS_PATH."/city.cache.php"; include PLUS_PATH."/com.cache.php"; include PLUS_PATH."/job.cache.php"; include PLUS_PATH."/user.cache.php"; include PLUS_PATH."/industry.cache.php"; include PLUS_PATH."/school.cache.php"; $array["schoolclass_name"] = $schoolclass_name; $array["comclass_name"] = $comclass_name; $array["city_name"] = $city_name; $array["city_index"] = $city_index; $array["user_classname"] = $userclass_name; $array["job_name"] = $job_name; $array["job_index"] = $job_index; $array["job_type"] = $job_type; $array["industry_name"] = $industry_name; return $array; } function fscacheget(){ include PLUS_PATH."/cityparent.cache.php"; include PLUS_PATH."/jobparent.cache.php"; $array["job_parent"] = $job_parent; $array["city_parent"] = $city_parent; return $array; } //将信息替换为缓存数组中内容 function array_action($job_info,$array=array()){ if(!empty($array)){ $comclass_name = $array["comclass_name"]; $city_name = $array["city_name"]; $industry_name = $array["industry_name"]; $job_name = $array["job_name"]; $schoolclass_name = $array["schoolclass_name"]; }else{ include PLUS_PATH."/city.cache.php"; include PLUS_PATH."/com.cache.php"; include PLUS_PATH."/job.cache.php"; include PLUS_PATH."/industry.cache.php"; include PLUS_PATH."/school.cache.php"; } $job_info[job_class_one] = $job_name[$job_info["job1"]]; $job_info[job_class_two] = $job_name[$job_info[job1_son]]; $job_info[job_class_three] = $job_name[$job_info[job_post]]; $job_info[job_exp] = $comclass_name[$job_info["exp"]]; $job_info[job_edu] = $comclass_name[$job_info[edu]]; $job_info[job_salary] = $comclass_name[$job_info[salary]]; // $job_info[job_number] = $comclass_name[$job_info[number]]; $job_info[job_mun] = $comclass_name[$job_info[mun]]; //$job_info[job_sex] = $comclass_name[$job_info[sex]]; // $job_info[job_age] = $comclass_name[$job_info[age]]; $job_info[job_type] = $comclass_name[$job_info[type]]; $job_info[job_marriage] = $comclass_name[$job_info[marriage]]; $job_info[job_report] = $comclass_name[$job_info[report]]; $job_info[job_city_one] = $city_name[$job_info[provinceid]]; $job_info[com_city] = $city_name[$job_info[com_provinceid]]; $job_info[job_pr] = $comclass_name[$job_info[pr]]; $job_info[job_city_two] = $city_name[$job_info[cityid]]; $job_info[job_city_three] = $city_name[$job_info[three_cityid]]; $job_info[job_hy] = $industry_name[$job_info[hy]]; $job_info[school_department] = $schoolclass_name[$job_info["school_department"]]; $job_info[school_level] = $schoolclass_name[$job_info[school_level]]; $job_info[school_categty] = $schoolclass_name[$job_info[school_categty]]; $job_info[schooltag] = $schoolclass_name[$job_info[schooltag]]; //$job_info[edate]=date("Y年m月d日",$job_info[edate]); if($job_info[lang]!=""){ $lang = @explode(",",$job_info[lang]); foreach($lang as $key=>$value){ $langinfo[]=$comclass_name[$value]; } $job_info[lang_info] = @implode(",",$langinfo); $job_info[lang] =$lang; }else{ $job_info[lang_info] =""; } if($job_info[welfare]!=""){ $welfare = @explode(",",$job_info[welfare]); foreach($welfare as $key=>$value){ //$welfareinfo[]=$comclass_name[$value]; $welfareinfo[]=$value; } $job_info[welfare_info] = @implode(",",$welfareinfo); $job_info[welfare] =$welfare; }else{ $job_info[welfare_info] =""; } return $job_info; } //将信息替换为缓存数组中内容(猎头) function lt_array_action($job_info,$array=array()){ if(!empty($array)){ $comclass_name = $array["comclass_name"]; $ltclass_name = $array["ltclass_name"]; $city_name = $array["city_name"]; $lthy_name = $array["lthy_name"]; $ltjob_name = $array["ltjob_name"]; }else{ include PLUS_PATH."/com.cache.php"; include PLUS_PATH."/city.cache.php"; include PLUS_PATH."/lt.cache.php"; include PLUS_PATH."/ltjob.cache.php"; include PLUS_PATH."/lthy.cache.php"; } $job_info["cityid_info"] = $city_name[$job_info["cityid"]]; $job_info["three_cityid_info"] = $city_name[$job_info["three_cityid"]]; //$job_info["salary_info"] = $ltclass_name[$job_info["salary"]]; $job_info["edu_info"] = $ltclass_name[$job_info["edu"]]; $job_info["exp_info"] = $ltclass_name[$job_info["exp"]]; $job_info["mun_info"] = $comclass_name[$job_info["mun"]]; $job_info["pr_info"] = $comclass_name[$job_info["pr"]]; $job_info["jobone"]=$ltjob_name[$job_info["jobone"]]; /*if($job_info["social"]!=""){ $social = @explode(",",$job_info["social"]); foreach($social as $key=>$value){ $social_info[]=$ltclass_name[$value]; } $job_info["social_info"] = $social_info; }else{ $job_info["social_info"] =""; }*/ $welfare=@explode(',', $job_info['welfare']); if($welfare){ foreach($welfare as $val){ $job_info['welfarename'][]=$ltclass_name[$val]; } } return $job_info; } //将信息替换为缓存数组中内容(兼职) function part_array_action($job_info,$array=array()){ if(!empty($array)){ $partclass_name = $array["partclass_name"]; $city_name = $array["city_name"]; }else{ include PLUS_PATH."/city.cache.php"; include PLUS_PATH."/part.cache.php"; } $job_info['job_salary_type'] = $partclass_name[$job_info['salary_type']]; //$job_info['job_sex'] = $partclass_name[$job_info['sex']]; $job_info['job_type'] = $partclass_name[$job_info['type']]; $job_info['job_billing_cycle'] = $partclass_name[$job_info['billing_cycle']]; $job_info['job_city_one'] = $city_name[$job_info['provinceid']]; $job_info['job_city_two'] = $city_name[$job_info['cityid']]; $job_info['job_city_three'] = $city_name[$job_info['three_cityid']]; return $job_info; } /*查询服务器所有数据库*/ //将系统数据库与用户数据库分开,更直观的显示? public function show_databases() { $this->query("show databases"); echo "现有数据库:" . $amount = $this->db_num_rows($rs); echo "
"; $i = 1; while ($row = $this->fetch_array($rs)) { echo "$i $row[Database]"; echo "
"; $i++; } } //以数组形式返回主机中所有数据库名 public function databases() { $this->connect(); $rsPtr = mysql_list_dbs($this->conn); $i = 0; $cnt = mysql_num_rows($rsPtr); while ($i < $cnt) { $rs[] = mysql_db_name($rsPtr, $i); $i++; } return $rs; } /*查询数据库下所有的表*/ public function show_tables($database_name) { $this->connect(); $this->query("show tables"); echo "现有数据库:" . $amount = $this->db_num_rows($rs); echo "
"; $i = 1; while ($row = $this->fetch_array($rs)) { $columnName = "Tables_in_" . $database_name; echo "$i $row[$columnName]"; echo "
"; $i++; } } /* mysql_fetch_row() array $row[0],$row[1],$row[2] mysql_fetch_array() array $row[0] 或 $row[id] mysql_fetch_assoc() array 用$row->content 字段大小写敏感 mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感 */ /*取得结果数据*/ public function mysql_result_li() { $this->connect(); return mysql_result($str); } /*取得记录集,获取数组-索引和关联,使用$row['content'] */ public function fetch_array($query="") { $this->connect(); if(!$query){ return @mysql_fetch_array($this->result, MYSQL_ASSOC); }else{ return @mysql_fetch_array($query, MYSQL_ASSOC); } } public function fetch_array_old($query="") { $this->connect(); if(!$query){ return @mysql_fetch_array($this->result); }else{ return @mysql_fetch_array($query); } } //获取关联数组,使用$row['字段名'] public function fetch_assoc() { $this->connect(); return mysql_fetch_assoc($this->result); } //获取数字索引数组,使用$row[0],$row[1],$row[2] public function fetch_row() { $this->connect(); return mysql_fetch_row($this->result); } //获取对象数组,使用$row->content public function fetch_Object() { $this->connect(); return mysql_fetch_object($this->result); } /*取得上一步 INSERT 操作产生的 ID*/ public function insert_id() { $this->connect(); return mysql_insert_id(); } //指向确定的一条数据记录 public function db_data_seek($id) { $this->connect(); if ($id > 0) { $id = $id -1; } if (!@ mysql_data_seek($this->result, $id)) { $this->show_error("SQL语句有误:", "指定的数据为空"); } return $this->result; } // 根据select查询结果计算结果集条数 public function db_num_rows() { $this->connect(); if ($this->result == null) { if ($this->show_error) { $this->show_error("SQL语句错误", "暂时为空,没有任何内容!"); } } else { return mysql_num_rows($this->result); } } // 根据insert,update,delete执行结果取得影响行数 public function db_affected_rows() { $this->connect(); return mysql_affected_rows(); } public function show_error($message = "", $sql = "") { exit('FAILED: Sql Error!'); } //释放结果集 public function free() { @ mysql_free_result($this->result); } //数据库选择 public function select_db($db_database) { $this->connect(); return mysql_select_db($db_database); } //查询字段数量 public function num_fields($table_name) { $this->connect(); return mysql_num_fields($this->result); /* $this->query("select * from $table_name"); echo "
"; echo "字段数:" . $total = mysql_num_fields($this->result); echo "
";
		for ($i = 0; $i < $total; $i++) {
			print_r(mysql_fetch_field($this->result, $i));
		}
		echo "
"; echo "
"; */ } //取得 MySQL 服务器信息 public function mysql_server($num = '') { $this->connect(); switch ($num) { case 1 : return mysql_get_server_info(); //MySQL 服务器信息 break; case 2 : return mysql_get_host_info(); //取得 MySQL 主机信息 break; case 3 : return mysql_get_client_info(); //取得 MySQL 客户端信息 break; case 4 : return mysql_get_proto_info(); //取得 MySQL 协议信息 break; default : return mysql_get_client_info(); //默认取得mysql版本信息 } } //转义字符串 function escape_string($string){ $this->connect(); return mysql_real_escape_string($string); } //关闭数据库,垃圾回收机制 public function close() { if($this->conn){ @mysql_close($this->conn); } } //析构函数,自动关闭数据库,垃圾回收机制 public function __destruct() { if($this->conn){ if (!empty ($this->result)) { $this->free(); } @mysql_close($this->conn); } } //function __destruct(); function getmicrotime(){ list($usec, $sec) = @explode(" ", microtime()); return ((float)$usec + (float)$sec); } } ?>