mysqli二次封装 蛋疼 本来就面向对象 又封装了一次
程序员文章站
2022-04-27 16:36:42
...
php代码:
<?php //mysqli的DB 类 /** * */ class MYSQLI{ public $dbhost; //主机 public $dbuser; //用户 public $dbpassword; //密码 public $dbname; //数据库名称 public $dbport; //端口号 public $errno; //错误号 public $error; //错误内容 public $mysqli; //mysqli连接对象句柄 public $query; //query结果 public $result; //查询的结果集 public $aff_rows; //受影响的行数 public $num_rows; //查询结果条数 function __construct(){ //连接数据库 $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpassword, $this->dbname, $this->dbport); if($this->mysqli->connect_error){ die('Connect Error ('.$this->mysqli->connect_errno.')'.$this->mysqli->connect_error); } } //执行 dml 操作语句 function dml($sql){ $this->query = $this->mysqli->query($sql); } //取得受影响的行数 function affected_rows(){ $this->aff_rows = $this->mysqli->affected_rows; } //执行 dql 语句 function dql($sql){ $this->result = $this->mysqli->query($sql); } //取得查询结果条数 function num_rows(){ $this->num_rows = $this->result->num_rows; } //取得查询结果集 function fetch_object(){ //以对象形式返回 return $obj = $this->result->fetch_object(); } } ?>