推薦(免費):PHP7
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 class="brush:php;toolbar:false">'; print_r($rows); echo ''; ?>
相關免費學習推薦:mysql影片教學
以上是介紹PHP7.3.5封裝類別存取mysql資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!