您好,欢迎访问一九零五行业门户网

封装的 mysql类, 和sql语句生成类

host = $server['host']; $this->user = $server['user']; $this->pwd = $server['pwd']; $this->port = $server['port']; $this->dbname = $server['dbname']; $this->connect(); }//end of function public static function getinstance($server) { if( self::$isconnect ) { return self::$handle; } self::$handle = new self($server); self::$isconnect = true; return self::$handle; }//end of funtion private function connect() { $this->resource = mysql_connect($this->host.':'.$this->port, $this->user, $this->pwd ) or $this->error("connect fail"); mysql_select_db($this->dbname, $this->resource); return true; } private function getsql($sql) { $operate = array('insert', 'delete', 'update', 'select', 'create'); return $sql; } public function query($sql) { $sql = $this->getsql($sql); mysql_query("set names utf8"); $query_result = mysql_query($sql, $this->resource) or $this->error("query fail"); return $query_result; } public function getqueryresult($sql) { $query_result = $this->query($sql); $result = array(); if( !$query_result ) { return $result; } while ( $row = mysql_fetch_assoc($query_result) ) { $result[] = $row; } $result['rows'] = mysql_num_rows($query_result); $query_result = null; return $result; } public function getinsertresult($sql) { $query_result = $this->query($sql); if( !$query_result ) { return false; } return mysql_insert_id($this->resource); } public function getupdateresult($sql) { $query_result = $this->query($sql); if( !$query_result ) { return false; } return mysql_affected_rows($this->resource); } public function getdeleteresult($sql) { return $this->getupdateresult($sql); } public function close_connect() { self::$handle = null; self::$isconnect = false; mysql_free_result($this->resource); $this->resource = null; } private function error($msg='') { $msg = "$msg--->>".mysql_error(); die($msg); } }//end of class ?>
2. [文件] dbtool.class.phpprimary_key = $primary_key; } } public function getinsertsql($data, $table) { $sql = $key_str = $value_str = ""; foreach($data as $key=>$value) { $key_str .= "{$key}, "; $value_str .= "'{$value}', "; } $key_str = trim($key_str, ', '); $value_str = trim($value_str, ', '); $sql = "insert into {$table}({$key_str}) values({$value_str})"; $data=null; $key_str=null; $value_str=null; return $sql; } public function getupdatesql($data, $table) { $pk = $this->primary_key; $id = $data[$pk]; unset($data[$pk]); $sql = $key_value = ""; foreach($data as $key=>$value) { $key_value .= "{$key}='{$value}', "; } $key_value = trim($key_value, ', '); $sql = "update {$table} set {$key_value} where $pk='{$id}'"; $data=null; $key_value=null; return $sql; } public function getquerysql($condition, $table) { $field = empty($condition['field']) ? '*': $condition['field']; $sql = "select {$field} from {$table} "; if( isset($condition['where']) ) { $sql .= "where {$condition['where']} "; } if( isset($condition['groupby']) ) { $sql .= "group by {$condition['groupby']} "; } if( isset($condition['orderby']) ) { $sql .= "order by {$condition['orderby']} "; } if( isset($condition['limit']) ) { $sql .= "limit {$condition['limit']} "; } $condition=null; return $sql; } public function getdeletesql($id, $table) { $pk = $this->primary_key; $sql = "delete from {$table} where $pk='{$id}' "; return $sql; } } ?>
其它类似信息

推荐信息