zoukankan      html  css  js  c++  java
  • 用类写一个连接数据库的方法

    首先需要两个php文件:

    index.php

    Newdb.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>
    

      

     

     
    Newdb.class.php代码如下
    <?php
    class Newdb{
        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();
        }
  • 相关阅读:
    数字签名(代码签名)流程
    (转)__cdecl __fastcall与 __stdcall
    装修主材
    ATL 获取flash信息
    Windows结构化异常
    格式化HRESULT获取对应文本
    which type of VS files should be committed into a version control system
    读Windows核心编程-5-作业
    IE WebBrowser事件触发
    Windows 结构化异常
  • 原文地址:https://www.cnblogs.com/awdsjk/p/9764354.html
Copyright © 2011-2022 走看看