足球比赛-2

优化PHP代码,将数据库基本操作封装起来。

1、MySQLDB.class.php类代码如下:

<?php 
    //类名习惯上推荐使用与文件名相似的名字
    //定义一个mysql连接类,该类可以连接mysql数据库
    //并实现其单例模式
    //该类的功能还能够完成如下的mysql基本操作
    //执行普通的增删改非返回结果集的语句
    //执行SELECT语句并返回3种类型的数据
    //多行结果(二维数组),单行结果(一维数组)
    //单行单列(单个数据)
class MySQLDB{
    public $host;
    public $port;
    public $username;
    public $password;
    public $charset;
    public $dbname;

    //连接结果(资源类型)
    private static $link;

    //数据库连接结果(资源类型)
    private $resourc;

    //获取类的实例
    public static function getInstance($config){
        if(!isset(self::$link)){
                self::$link=new self($config);
        }
        return self::$link;
    }
    //构造函数:禁止new
    private function __construct($config){
        //初始化数据
        $this->host=isset($config['host'])?$config['host']:'localhost';
        $this->port=isset($config['port'])?$config['port']:'3306';
        //echo '主机名:'.$this->host;(测试使用)
        $this->username=isset($config['username'])?$config['username']:'root';
        $this->password=isset($config['password'])?$config['password']:'';
        $this->charset=isset($config['charset'])?$config['charset']:'utf8';
        $this->dbname=isset($config['dbname'])?$config['dbname']:'';
        //连接数据库
        $this->connect();
        //设置连接编码
        $this->setCharset($this->charset);
        //选定数据库
        $this->selectDb($this->dbname);
    }

    //连接数据库
    public function connect(){
        $this->resourc=mysql_connect("$this->host:$this->port","$this->username","$this->password") or die("连接数据库失败!");
    }

    //设置连接编码
    public function setCharset($charset){
        mysql_set_charset($charset,$this->resourc);
    }

    //选定数据库
    public function selectDb($dbname){
        mysql_select_db($dbname,$this->resourc);
    }

    //功能:执行最基本的sql语句,成功返回执行结果,否则直接结束
    public function query($sql){
        if(!$result=mysql_query($sql,$this->resourc)){
            echo "<br/>执行失败。";
            echo "<br/>失败的sql语句为:".$sql;
            echo "<br/>出错信息为:".mysql_error();
            echo "<br/>出错行号为:".mysql_errno();
            die();
        }
        return $result;
    }

    //功能:执行select语句,返回二维数组
    //参数:$sql字符串类型 select语句
    public function getAll($sql){
        $result=$this->query($sql);
        $arr=array();        //空数组
        while($rec=mysql_fetch_assoc($result)){
            $arr[]=$rec;    //这样就形成二维数组
        }
        return $arr;
    }
}
?>

2、前台代码

<?php 
//比赛列表
header("Content-type:text/html;charset=utf-8");

//通过数据操作,将比赛列表需要的数据处理
//初始化MySQL
$config=array(
        'host'=>'127.0.0.1',
        'port'=>'3306',
        'username'=>'root',
        'password'=>'123',
        'charset'=>'utf8',
        'dbname'=>'db_match'
    );
require "./MySQLDB.class.php";
$dao=MySQLDB::getInstance($config);    //dao,Database ACCESS Object 数据库操作对象(dao层)
//echo gettype($dao);
//获取比赛列表数据,从前面把sql语句拷贝过来
$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` m join team t1 on m.t1_id =t1.t_id join team t2 on m.t2_id =t2.t_id";
//$sql="select * from player`";
//var_dump($dao);
$match_list=$dao->getAll($sql);    //获取多行比赛结果,类型为数组
//print_r($match_list);

?>
<!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 };?>
    </table>
</body>
</html>

3、浏览器显示效果

  

猜你喜欢

转载自www.cnblogs.com/mnhome/p/9852376.html