php连接操作mysql原生封装类与使用方法案例
2020-07-05 重庆LiSEO工作室
php连接操作mysql原生封装类源码Db.class.php如下:
<?php class Db{ protected static $db=null; private static $config =array( 'hostname'=>'localhost', 'username'=>'root', 'password'=>'root', 'dbname'=>'school', 'port'=>3306, 'charset'=>'utf8', ); /** * [构造函数,返回值给$con] * @param [string] $hostname [主机名] * @param [string] $username[用户名] * @param [string] $password[密码] * @param [string] $dbname[数据库名] * @param [string] $charset[字符集] * @return [null] */ public function __construct(array $config=array()){ $con = array_merge(self::$config,$config); // 若用户未传递参数则使用默认值 self::$db || self::connect($con); // 若未实例化mysqli对象,则进行实例化 } //连接服务器 private static function connect($con){ try{ //实例化mysqli self::$db = new mysqli($con['hostname'],$con['username'],$con['password'],$con['dbname'],$con['port']); } catch (mysqli_sql_exception $ex) { exit('数据库连接失败'); } self::$db->set_charset($con['charset']); //设置字符编码 } /** * [getAll 获取所有数据] * @param [string] $sql [sql语句] * @return [array] [返回二维数组] */ public function getAll($sql){ $result = self::$db->query($sql); $data = array(); if($result && $result->num_rows > 0){ $data=$result->fetch_all(MYSQLI_ASSOC); } return $data; } /** * [getOne 获取单条数据] * @param [string] $sql [sql语句] * @return [array] [返回一维数组] */ public function getOne($sql){ $result = self::$db->query($sql); $data = array(); if($result && $result->num_rows > 0){ $data = $result->fetch_assoc(); } return $data; } /** * [getOne 获取单条数据] * @param [string] $table [表名] * @param [string] $data [由字段名当键,属性当键值的一维数组] * @return [type] [返回false或者插入数据的id] */ public function insert($table,$data){ $str = ''; $str .="INSERT INTO `$table` "; $str .="(`".implode("`,`",array_keys($data))."`) "; $str .=" VALUES "; $str .= "('".implode("','",$data)."')"; $res = self::$db->query($str); if($res && self::$db->affected_rows>0){ return self::$db->insert_id; //return true; //当处理页面不需要具体的参数时,可使用return true }else{ return false; } } /** * [update 更新数据库] * @param [string] $table [表名] * @param [array] $data [更新的数据,由字段名当键,属性当键值的一维数组] * @param [string] $where [条件,‘字段名'=‘字段属性'] * @return [type] [更新成功返回影响的行数,更新失败返回false] */ public function update($table,$data,$where){ $sql = 'UPDATE '.$table.' SET '; foreach($data as $key => $value){ $sql .= "`{$key}`='{$value}',"; } $sql = rtrim($sql,','); $sql .= " WHERE $where"; $res = self::$db->query($sql); if($res && self::$db->affected_rows > 0){ return self::$db->affected_rows; //return true; //当处理页面不需要具体的参数时,可使用return true }else{ return false; } } /** * [delete 删除数据] * @param [string] $table [表名] * @param [string] $where [条件,‘字段名'=‘字段属性'] * @return [type] [成功返回影响的行数,失败返回false] */ public function delete($table,$where){ $sql = "DELETE FROM `{$table}` WHERE {$where}"; $res = self::$db->query($sql); if($res && self::$db->affected_rows > 0){ return self::$db->affected_rows; }else{ return false; } } //阻止克隆 private function __clone(){} //关闭数据库 function __destruct(){ self::$db->close(); } }
使用方法案例代码dbtest.php如下:
<?php require_once 'public/Db.class.php'; $db=new Db(); //实例化数据库操作类 //获取一条数据 $sql = "SELECT * FROM student"; $rs = $db->getOne($sql); var_dump($rs); //获取多条数据 $sql = "SELECT * FROM student"; $rs = $db->getAll($sql); var_dump($rs); //插入数据 $data = array( 'stuid'=>'18003', 'name'=>'洪七', 'age'=>18, 'sex'=>'女', ); $rs = $db->insert('student',$data); var_dump($rs); //更新数据 $data = array( 'stuid'=>'18004', 'name'=>'朱重八', 'age'=>118, 'sex'=>'男', ); $where ="id=23"; $rs = $db->update('student',$data,$where); var_dump($rs); //删除数据 $where = "id=22"; $rs = $db->delete('student',$where); var_dump($rs);