PHP7.3.5如何封装类高效访问MySQL数据库?

2026-04-03 11:401阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计254个文字,预计阅读时间需要2分钟。

PHP7.3.5如何封装类高效访问MySQL数据库?

推荐(免费):PHP7+PHP7.3.5封装类访问MySQL数据库+Model.class.php

// 构造方法,以及表名赋值 public function __construct($t) { $this->table=$t; }}

推荐(免费):PHP7

PHP7.3.5封装类访问mysql数据库

PHP7.3.5如何封装类高效访问MySQL数据库?

Model.class.php

<?php class Model{ // 表名属性 public $table; public $connect; //构造方法以及表名赋值 public function __construct($t){ $this->table=$t; } private function conn(){ $servername = "localhost"; $username = "root"; $password = "133"; $dbname = "myweb"; // 创建连接 if($conn=mysqli_connect($servername, $username, $password, $dbname)){ return $this->connect=$conn; } } //select查询 public function select(){ $this->conn(); $sql="SELECT * FROM {$this->table}"; $rst=mysqli_query($this->connect,$sql); while($row=mysqli_fetch_assoc($rst)){ $rows[]=$row; } return $rows; } } ?>

function.inc.php

<?php function M($name){ return new Model($name); } ?>


index.php

<?php include "Model.class.php"; include 'function.inc.php'; $rows=M('user')->select(); echo '<pre>'; print_r($rows); echo '</pre>'; ?>

相关免费学习推荐:mysql视频教程

本文共计254个文字,预计阅读时间需要2分钟。

PHP7.3.5如何封装类高效访问MySQL数据库?

推荐(免费):PHP7+PHP7.3.5封装类访问MySQL数据库+Model.class.php

// 构造方法,以及表名赋值 public function __construct($t) { $this->table=$t; }}

推荐(免费):PHP7

PHP7.3.5封装类访问mysql数据库

PHP7.3.5如何封装类高效访问MySQL数据库?

Model.class.php

<?php class Model{ // 表名属性 public $table; public $connect; //构造方法以及表名赋值 public function __construct($t){ $this->table=$t; } private function conn(){ $servername = "localhost"; $username = "root"; $password = "133"; $dbname = "myweb"; // 创建连接 if($conn=mysqli_connect($servername, $username, $password, $dbname)){ return $this->connect=$conn; } } //select查询 public function select(){ $this->conn(); $sql="SELECT * FROM {$this->table}"; $rst=mysqli_query($this->connect,$sql); while($row=mysqli_fetch_assoc($rst)){ $rows[]=$row; } return $rows; } } ?>

function.inc.php

<?php function M($name){ return new Model($name); } ?>


index.php

<?php include "Model.class.php"; include 'function.inc.php'; $rows=M('user')->select(); echo '<pre>'; print_r($rows); echo '</pre>'; ?>

相关免费学习推荐:mysql视频教程