PHP面向对象Mysql方法类封装代码
给大家分享一个mysql系列的PHP封装类代码,直接调用即可,封装函数有:
1:链接数据库方法
2:发送sql语句方法
3:获取全部数据方法
4:获取一条数据方法
5:获取单个数据方法
一共集成了5个方法,大家直接使用即可,也可以用来学习。
config.php
<?php //配置数据库信息 $cfg = array( 'host' => '127.0.0.1', 'user' => 'jiami', 'pwd' => '123456', 'db' => 'jiami', 'charset' => 'utf8' ); return $cfg; //作者无陌然qqx ?>
mysql.class.php
<?php class Mysql{ public function __construct(){ $this->conn(); } public $link; //链接数据库信息 public function conn(){ $cfg = include('./config.php'); $this->link = new mysqli($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']); $this->query('use names'.$cfg['charset']); } //发送一条sql语句 public function query($sql){ // $lian = $this->conn(); return $this->link->query($sql); } //获取全部数据 public function getAll($sql){ $res = $this->query($sql); $data = []; while ( $row = $res->fetch_assoc() ) { $data[] = $row; } return $data; } //获取一条数据 public function getRow($sql){ $res = $this->query($sql); $row = $res->fetch_assoc(); return $row; } //获取单个数据 public function getOne($sql){ $res = $this->query($sql); $row = $res->fetch_row()[0]; return $row; } } $mysql = new Mysql(); print_r($mysql -> getOne('select * from xinxi')); //作者无陌然qqx ?>
资源均来自第三方,谨慎下载,前往第三方网站下载 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com