<?php class Mysql { private $mysqli; private static $ins; private function __construct(){ $this->mysqli = new mysqli('192.168.88.10','root','root','blog'); if(mysqli_connect_errno()) { printf('%s',mysqli_connect_error()); $this->mysqli = false; exit; } $this->mysqli->set_charset('gbk'); } public static function getMysql() { if(!(self::$ins instanceOf self)) { self::$ins = new self(); } return self::$ins; } public function mQuery($sql) { return $this->mysqli->query($sql); } public function mGetAll($sql) { $rs = $this->mQuery($sql); if(!$rs) { return false; } $data = array(); while($row = $rs->fetch_assoc()) { $data[] = $row; } $rs->close(); return $data; } public function mGetRow($sql) { $rs = $this->mQuery($sql); if(!$rs) { return false; } $row = $rs->fetch_assoc(); $rs->close(); return $row; } public function mGetOne($sql) { $rs = $this->mQuery($sql); if(!$rs) { return false; } $row = $rs->fetch_row(); $rs->close(); return $row[0]; } /* insert into cat(catname,num) values('aa',0); update cat set catname='aa',num=900 where cat_id = 10; */ public function mExec($table,$data,$act='insert',$where='0') { $sql = ''; if($act == 'insert') { $sql .= 'insert into '.$table .'('; $sql .= implode(',',array_keys($data)).') '; $sql .= ' values("'; $sql .= implode('","',array_values($data)).'")'; }elseif($act == 'update') { $sql .= 'update '.$table.' set '; foreach($data as $k=>$v) { $sql .= $k.'="'.$v.'",'; } $sql = rtrim($sql,','); $sql .= ' where '.$where; } return $this->mQuery($sql); } public function close(){ if($this->mysqli) { $this->mysqli->close(); } } public function __destruct() { $this->close(); } } /* header('Content-type:text/html;charset=gbk'); $db = Mysql::getMysql(); //$sql = 'select count(*) from cat where cat_id = 1'; //var_dump($db->mGetOne($sql)); $data = array('catname'=>'testaa','num'=>100); $db->mExec('cat',$data,'update','cat_id=1'); */