操作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"]; }}?>
- 操作MYSQL的完美解决方案
- windows命令行下对mysql 操作中文乱码完美解决方案
- MySQL ERROR2013完美解决方案
- Java操作Excel完美解决方案
- mysql负载均衡完美解决方案
- 关于mysql中无法显示中文的完美解决方案
- mssql到mysql项目的转型完美解决方案剖析
- 关于mysql中无法显示中文的完美解决方案
- 【mysql】Mysql中文乱码问题完美解决方案
- C#操作MySQL中文乱码的解决方案
- 用Java操作Microsoft Excel完美解决方案
- Mysql中文乱码问题完美解决方案
- Mysql 中文乱码 问题 完美 解决方案 详解!
- Mysql中文乱码问题完美解决方案
- Mysql中文乱码问题完美解决方案
- Mysql中文乱码问题完美解决方案
- Mysql中文乱码问题完美解决方案
- Mysql中文乱码问题完美解决方案
- J2EE简介
- VC++实现不规则窗口说明
- 数据标准化/归一化方法
- 多线程学习笔记(一)
- 学习指针不可少的好文章(转载)
- 操作MYSQL的完美解决方案
- SQL Server 2005 常用配置
- Oracle 9i windows上的安装提示Temp空间不足
- 类初始化详细过程!
- emacs中显示行号
- 常用正则表达式
- [转载] S3C4510上嵌入式Web服务器-boa在uclinux下的的移植
- 我的主页
- Oracle编程高手箴言:位图索引(Bitmap Index)的故事