<?php //mysqli数据库通用类 class Mysqli_Oper { private $hostname='127.0.0.1'; private $username='root'; private $password='52ebook'; private $dbname='b_work'; private $conn; function __construct() { $this->conn=new mysqli($this->hostname,$this->username,$this->password,$this->dbname); if(mysqli_connect_errno()) { echo mysqli_connect_error(); die; } $this->conn->set_charset('utf8'); } //获取所影响的行数 function QueryAffected($sql) { $this->conn->query($sql); return $this->conn->affected_rows; } //查询整个数据集 function QueryAll($sql,$result_mode='assoc') { $result=$this->conn->query($sql); $table; switch($result_mode) { case 'assoc': $table=$result->fetch_all(MYSQLI_ASSOC); break; case 'num': $table=$result->fetch_all(MYSQLI_NUM); break; case 'both': $table=$result->fetch_all(MYSQLI_BOTH); break; default: $table=$result->fetch_all(MYSQLI_ASSOC); break; } return $table; } //查询第一行数据 function QueryFirstRow($sql) { $result=$this->conn->query($sql); return $result->fetch_array(MYSQLI_ASSOC); } //获取上一次插入的id号 function QueryInsertid($sql) { $this->conn->query($sql); return $this->conn->insert_id; } //绑定参数,防止sql注入 function PreExecSql($sql,$par_array) { $pare=$this->conn->stmt_init(); $pare->prepare($sql); $pare->bind_param("iisi",$a,$b,$c,$d); for($n=0;$n<sizeof($par_array);$n++) { $a=$par_array[$n][0]; $b=$par_array[$n][1]; $c=$par_array[$n][2]; $d=$par_array[$n][3]; $pare->execute(); } $pare->close(); } function __destruct() { $this->conn->close(); } } ?>