• 20181009 用一个类封装一个连接数据库的方法


    需要两个PHP文件        

    index.PHP

    mysql.class.PHP

    index.PHP代码

    <?php
    # 比赛列表
    header('Content-Type: text/html; charset=utf-8');
    include "mysql.class.php";
    $config=[
    	"host"=>"localhost",
    	"name"=>"root",
    	"pwd"=>"",
    	"DbName"=>"z_075mvc"
    ];
    $db=new mysqlDb($config);
    //获得比赛列表数据
    $sql = "select t1.t_name as t1_name, m.t1_score, m.t2_score, t2.t_name as t2_name, m.m_time from `match` as m left join `team` as t1 ON m.t1_id = t1.t_id  left join `team` as t2 ON m.t2_id=t2.t_id";
    $match_list = $db->getAssoc($sql);
    
    ?>
    <!-- 利用HTML代码展示数据 -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>比赛列表</title>
    </head>
    <body>
    
    <table>
    <tr>
    <th>球队一</th><th>比分</th><th>球队二</th><th>时间</th>
    </tr>
    <?php foreach($match_list as $row) : ?>
    	<tr>
    	<td><?php echo $row['t1_name'];?></td>
    	<td><?php echo $row['t1_score'];?>:<?php echo $row['t2_score'];?></td>
    	<td><?php echo $row['t2_name'];?></td>
    	<td><?php echo date('Y-m-d H:i', $row['m_time']);?></td>
    	</tr>
    <?php endForeach;?>
    </table>	
    </body>
    </html>
    

      

    mysql.class.PHP代码

    <?php
    class mysqlDb{
        public $host; //服务器地址
        public $post=3306; //端口
        public $name; //用户名
        public $pwd; //m密码
        public $DbName; //数据库名称
        public $charset;//默认编码
        
        public $link;//数据库连接对象
        public $res; //资源   返回结果集
        
        function __construct($config){
            /*初始化参数*/
            $this->host = $config["host"] ? $config["host"] : "localhost";
            $this->name = $config["name"] ? $config["name"] : "root";    
            $this->pwd = $config["pwd"] ? $config["pwd"]  : "";
            $this->DbName = $config["DbName"] ?  $config["DbName"] : "mysql";
            $this->charset = $config["charset"] ?$config["charset"] : "utf8" ;
            
            /*连接数据库  设置字符集*/
            $this->connectMysql();
            $this->setCharSet();
        }
        
        /*连接数据库*/
        function connectMysql(){
            $this->link = new MySQLi($this->host,$this->name,$this->pwd,$this->DbName);
            !mysqli_connect_error() or die("连接数据库失败");
        }
        
        /*设置字符集*/
        function setCharSet(){
            $this->link->query("set names ".$this->charset );
        }
        
        /*执行sql语句的方法*/
        function DbQuery($sql){
            $this->res = $this->link->query($sql);
            if(!$this->res){
                echo ("<br />执行失败。");
                echo "<br />失败的sql语句为:" . $sql;
                echo "<br />出错信息为:" . mysqli_error($this->link);
                echo "<br />错误代号为:" . mysqli_errno($this->link);
                die;
            }
            return $this->res;
        }
       //返回字符串
        function getStr($sql){
            $zhi = $this->DbQuery($sql);
            //将结果集转为字符串
            $arr = $zhi->fetch_all();
            $brr = array();
            foreach($arr as $v){
                $brr[] = implode(",",$v);
            }
            return implode("^",$brr);
        }
    
        //返回json
        function getJson($sql){
            $zhi = $this->DbQuery($sql);
            $brr = array();
            while($row = $zhi->fetch_assoc()){
                $brr[] = $row;
            }
            return json_encode($brr);
        }
        /*返回关联数组*/
        function getAssoc($sql){
            $zhi = $this->DbQuery($sql);
            $brr = array();
            while($row = $zhi->fetch_assoc()){
                $brr[] = $row;
            }
            return $brr;
        }
        //返回索引数组
        function getAttr($sql){
            $zhi = $this->DbQuery($sql);
            return $zhi->fetch_all();
        }
    
    }

    四个返回数据的方式   想用哪个都可以调

    效果图

  • 相关阅读:
    Redis 如何保证缓存与数据库双写时的数据一致性
    Redis 缓存雪崩和缓存穿透问题
    Redis 的并发竞争 Key 问题
    【转】intelliJ IDEA集成checkStyle
    【转】hadoop深入研究:(十一)——序列化与Writable实现
    【转】Hadoop在MapReduce中使用压缩详解
    【转】JDK工具jinfo用法详解
    【转】JVM统计监控工具-jstat
    【转】jps命令使用
    基于MLlib的机器学习--协同过滤与推荐
  • 原文地址:https://www.cnblogs.com/sp1234/p/9758554.html
Copyright © 2020-2023  润新知