时间:2021-07-01 10:21:17 帮助过:30人阅读
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'));