
前言:
PHP面向对象Mysql方法类封装实例代码,如果对你有帮助就看看吧。
正文:
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; //作者无陌然qq2633544207 ?>
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'));
//作者无陌然qq2633544207
?>
声明:所有内容均收集于网络,收集的内容仅供内部学习和讨论,建议您在下载后的24个小时之内从您的电脑或手机中删除上述内容,如果您喜欢该内容,请支持并购买正版资源。如若本站内容侵犯了原著者的合法权益,请联系邮箱3641180084@qq.com,我们将及时处理。

评论(0)