操作MYSQL的完美解决方案

<?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"];  } } ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yagas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值