Home > Backend Development > PHP7 > body text

Introducing PHP7.3.5 encapsulation class to access mysql database

coldplay.xixi
Release: 2023-02-17 19:02:01
forward
2063 people have browsed it

Introducing PHP7.3.5 encapsulation class to access mysql database

Recommended (free): PHP7

PHP7.3.5 encapsulation class Access mysql database

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;
    }
}
?>
Copy after login

function.inc.php

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

?>
Copy after login

index.php

<?php
include "Model.class.php";
include &#39;function.inc.php&#39;;



$rows=M(&#39;user&#39;)->select();

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($rows);
echo &#39;
'; ?>
Copy after login

Related free learning recommendations: mysql video tutorial

The above is the detailed content of Introducing PHP7.3.5 encapsulation class to access mysql database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template