操作MYSQL的完美解决方案

来源:互联网 发布:内饰 防御机制 知乎 编辑:程序博客网 时间:2024/04/30 05:18

<?php/*############################################## class: ieb_database## author: yagas## email: yagas60@21cn.com## public: 2007-10-3## site: http://www.iebsoft.com############################################*/class ieb_database{var $db     = null;var $prefix = null;

//构造函数function ieb_database($params){$host     = null;$user     = null;$password = null;$database = null;$charset  = 'latin1';$prefix   = '';

//对变量进行赋值foreach($params as $_key => $_val) { $$_key = $_val;}

$this->db = mysql_connect($host, $user, $password);mysql_select_db($database, $this->db);

//对于高版本MYSQL进行默认编码设置@mysql_query("set names '$charset'", $this->db);$this->prefix = $prefix;}

//取得数据表名称function table($name){return $this->prefix . $name;}

//释放占用的内存function free(){mysql_close($this->db);unset($this->db);}

//执行SQL语句function query($sql){$result = mysql_query($sql, $this->db);if(!$result){  return mysql_error();  exit;}

return $result;}

//对数据进行md5加密function password($pwd){$password = strtolower(md5($pwd));return $password;}

 

//返回插入数据时自动编号值function insert_id(){  return mysql_insert_id($this->db);}

//insert datafunction insert($table, $fieldArr){  $tableName = $this->table($table);  foreach($fieldArr as $_key=>$_val){   $val      = is_string($_val)? mysql_real_escape_string($_val):$_val;   $fields[] = "`". $_key ."`";   $values[] = "'". $val ."'";  }    $sql = "insert into `$tableName`(". implode(",", $fields) .") values (". implode(",", $values) .")";  return $this->query($sql);}

 //updatefunction update($table, $data, $condition=null){  $tableName = $this->table($table);  if(!is_array($data)) { exit;  }    foreach($data as $_key=>$_val){   $val = is_string($_val)? mysql_real_escape_string($_val):$_val;   $updates[] = "`{$_key}`='{$_val}'";  }    $sql = "update `$tableName` set ". implode(",", $updates);    if(is_array($condition) && count($condition)>0){ $sql .= " where ". implode(" and ", $condition);  }    return $this->query($sql);}

//delete datafunction delete($tableName, $condition=null){ $tableName = $this->table($table); $sql = "DELETE FROM `{$tableName}`";  if(is_array($condition) && count($condition)>0){  $sql .= "WHERE " . implode(" and ", $condition); }  return $this->query($sql);}

//return the first select.function find($table, $condition="", $order="", $fields="*") { $table = $this->table($table);  $sql = "SELECT {$fields} FROM `{$table}`";   if(is_array($condition)) {  $whereby = implode(" AND ", $condition);  $sql .= " WHERE " . $whereby; }  if($order) {  $sql .= " ORDER BY {$order}"; }  $sql .= " limit 0, 1";  $result = $this->query($sql);  if(mysql_num_rows($result) == 0) {  return false;  exit; }  return mysql_fetch_assoc($result);}

//return the select all.function findAll($table, $condition="", $order="", $limit=5, $fields="*") { $table = $this->table($table);  $sql = "SELECT {$fields} FROM `{$table}`";   if(is_array($condition)) {  $whereby = implode(" AND ", $condition);  $sql .= " WHERE " . $whereby; }  if($order) {  $sql .= " ORDER BY {$order}"; }  $sql .= " limit 0, {$limit}";  $result = $this->query($sql); while($row = mysql_fetch_assoc($result)) {  $data[] = array_map(null, $row); }  return $data;}

 

//返回记录总和function findCount($table, $condition="") { $table = $this->table($table);  $sql = "SELECT count(*) as `count` FROM `{$table}`";   if(is_array($condition)) {  $whereby = implode(" AND ", $condition);  $sql .= " WHERE " . $whereby; } $result = $this->query($sql); $result = mysql_fetch_assoc($result); return $result["count"]; }}?>