本文主要是介绍mysqli 面向对象操作数据库的封装类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
class db{private $mysqli;private $options;private $tableName;function __construct($tableName){$this->tableName=$tableName;$this->db();}private function db(){$this->mysqli=new mysqli('localhost','root','','表名字');$this->mysqli("set names gbk");}function fields($fieldsArr){if(empty{$fieldsArr}){$this->options['fields']='';}if(is_array($fieldsArr)){$this->options['fields']=implode(',',$fieldsArr);}else{$this->options['fields']=$fieldsArr;}return $this;}function order($stt){$this->options['order']="order by ".$str;return $this;}function select(){$sql="select {$this->options[fields]} from {$this->tableName} {$this->options['order']}";return $this->query($sql);}private function query($sql){$result=$this->mysqli->query($sql);$rows=array();while($row=$result->fetch_assoc()){$rows[]=$row;}return $rows;}private function close(){$this->mysqli->close();}private function __destruct(){$this->close();}
}$channel=new db('channel');
$channelInfo=$channel->fields('id,name,parent_id')->select();
print_r($channel);
这篇关于mysqli 面向对象操作数据库的封装类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!