<?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 data function 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); }
//update function 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 data function 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"]; } } ?>