mysql 类 待更新完善_MySQL
程序员文章站
2022-05-23 22:16:56
...
connect($host,$user,$pwd,$db); } //数据库连接 public function connect($host,$user,$pwd,$db) { $this->con = @mysql_connect($host,$user,$pwd,true); if(!$this->con) { echo '连接失败'.mysql_error(); die(); } if(!mysql_select_db($db,$this->con)) { echo '库选择失败'.mysql_error(); } mysql_query('set names utf8'); } //查询 Public function query($sql) { $rs = mysql_query($sql,$this->con); if(!$rs) { echo 'sql语句错误'.mysql_error(); die(); } return $rs; } //单挑查询 public function find($sql) { $rt = mysql_fetch_assoc($this->query($sql)); return $rt; } //所有查询 public function findall($sql) { $r = $this->query($sql); $arr = array(); while(($row = mysql_fetch_assoc($r))!=false) { $arr[] = $row; } return $arr; } //查询记录数 public function count($sql) { $r = $this->query($sql); return mysql_num_rows($r); } //更新数据 public function update($table,$data,$condition) { $sql = 'update '.$table.' set '; while(list($k,$v) = each($data)) { $sql .= $k.' = "'.$v.'"'.','; } $sql = substr($sql,0,-1); $sql .=' where 1=1'; while(list($key,$val) = each($condition)) { $sql .= ' and '.$key.' = "'.$val.'"'; } echo $sql; $this->query($sql); } //删除数据 //condition array($key=>$val,.......) Public function del($table,$condition) { //$condition = array('id'=>12,'name'=>'zj'); $sql = 'delete from '.$table.' where 1=1'; while(list($key,$val) = each($condition)) { $sql .= ' and '.$key.' = "'.$val.'"'; } $this->query($sql); }}$db = new Mysql('192.168.0.230','root','fpdev','oa');//$one = print_r($db->find('select * from users'));//$all = print_r($db->findall('select * from users'));//echo $db->count('select * from users');//$db->del('user_bak',array('id'=>1019,'username'=>'katherine'));$db->update('user_bak',array('username'=>'zhangsan'),array('id'=>1020,'username'=>'lq'));
上一篇: 修改IE查看源代码编辑器
下一篇: PHP抽象类 介绍