class PDOHelper{ public static $db =null;// new PDO('mysql:host=192.168.1.68;dbname=test','root','123456'); public static function _connect(){ try { if(!isset($db)){ self::$db= new PDO('mysql:host=192.168.1.68;dbname=test','root','123456'); self::$db->query("SET NAMES 'utf8';"); } } catch (PDOException $e) { throw new Exception($e->getMessage(), $e->getCode()); } } public static function query($sql,$returnAffectCount=false){ self::_connect(); $st = self::$db->query($sql); if(false===$st){ $rs=false; }else if($returnAffectCount){ $rs=$st->rowCount(); }else{ $st->setFetchMode(PDO::FETCH_ASSOC); $rs=$st->fetchAll(); } return $rs; } public static function exec($sql){ self::_connect(); $count =self::$db->exec($sql); return $count; } }