码迷,mamicode.com
首页 > 数据库 > 详细

38)PHP,获取数据库数据并在html中显示(晋级5)

时间:2017-08-14 12:12:00      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:tab   .com   function   index   body   foreach   res   发布   isset   

首先是我的文件关系:

技术分享

我的主php文件是index.php,我的配置文件php是BBB.php  我的数据库操作文件是  b.php    我的html文件是login.html

我的index.php代码展示:

1 <?php
2     $sql=select * from zixun;;
3     $config=include ./BBB.php;
4     include ./b.php;
5     
6     $shujuku=new db($config);
7     include ./login.html;

我的BBB.php代码展示:

1 <?php
2     return $config=array(
3         host=>"localhost",
4         user=>"root",
5         pwd=>"root",
6         dbname=>"thkphp5",
7         sql=>$sql);

我的b.php代码展示:

 1 <?php
 2     class db
 3     {
 4         public $host ;//= "localhost";//定义默认连接方式
 5         public $User;//= "root";//定义默认用户名
 6         public $Pwd;//= "root";//定义默认的密码
 7         public $Dbname ;//= "thkphp5";//定义默认的数据库名
 8         public $my_sql;
 9         public $link;
10         public $result;
11         
12         /*
13          * 构造函数
14          * 主机名,使用者,使用者密码,数据库的名字,查询语句
15          */
16         public function __construct($config) {
17         $this->host=$config[host];
18         $this->User=$config[user];
19         $this->Pwd=$config[pwd];
20         $this->Dbname=$config[dbname];
21         $this->my_sql=$config[sql];
22       $this->link=  $this->connect();
23       $this->result=  $this->Query($this->my_sql);
24         }
25         /*
26          * 数据库查询函数
27          * $sql   string   是你的查询语句
28          */
29         public function Query($sql)
30             //两个参数:sql语句,判断返回1查询或是增删改的返回
31         {
32             $db = $this->connect();
33             $r = $db->query($sql);
34             if (isset($r)) {
35                 return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
36             } else {
37                 return "数据库查询失败!";
38             }
39     
40     
41         }
42         /*
43          * 数据库连接函数
44          */
45         public function connect(){
46             $Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
47             return $Link;
48         }
49         
50     }
51 //    $sql=‘select * from zixun;‘;
52 //    $config=include ‘./BBB.php‘;
53 //   $shujuku=new db($config);
54 
55 
56 //  include ‘./login.html‘;
57 //var_dump($shujuku->result);
58 
59 ?>

我的login.html代码展示:

<!-- 模板文件,利用HTML代码展示数据 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>比赛列表</title>
</head>
<body>

<table>
    <tr>
        <th>ZX_id</th><th>ZX_name</th><th>ZX_fenlei</th><th>ZX_zuozhe</th><th>更新时间</th><th>浏览次数</th><th>发布状态</th>
    </tr>
    <?php foreach($shujuku->result as $row) : ?>
    <tr>
        <td><?php echo $row[0];?></td>
        <td><?php echo $row[1];?></td>
        <td><?php echo $row[2];?></td>
        <td><?php echo $row[3];?></td>
        <td><?php echo $row[4];?></td>
        <td><?php echo $row[5];?></td>
        <td><?php echo $row[6];?></td>
    </tr>
    <?php endForeach;?>
</table>
</body>
</html>

我的结果展示:
技术分享

我的数据库代码展示:

 

 1 CREATE DATABASE `thkphp5` ;
 2 use thkphp5 ;
 3 create table zixun(
 4     ZX_id int  not null auto_increment primary key comment 咨询ID号,
 5     ZX_name VARCHAR(80) NOT NULL COMMENT 咨询标题,
 6     ZX_fenlei varchar(80) not null  comment 资讯分类,
 7     ZX_zuozhe varchar(80)  not null  comment 资讯作者,
 8     gengxin_time DATETIME NOT NULL DEFAULT 2016-01-01 01:01:01 COMMENT 更新时间,
 9     liulan_cishu int NOT NULL  COMMENT 浏览次数,
10     fabu_zhuangtai VARCHAR(50) NOT NULL COMMENT 发布状态
11 )engine=MyISAM charset=utf8;
12 INSERT  into zixun(ZX_id, ZX_name, ZX_fenlei, ZX_zuozhe, gengxin_time, liulan_cishu, fabu_zhuangtai) values(10001, PHP, 理论, 王超, 2017-08-07 11:58:01, 100, 草稿);
13 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10002,C语言,理论,王超,2017-08-07 11:58:01,100,草稿);
14 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10003,JAVA语言,理论,王超,2017-08-07 11:58:01,100,草稿);
15 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10004,Mysql语言,理论,王超,2017-08-07 11:58:01,100,草稿);
16 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10005,html,理论,王超,2017-08-07 11:58:01,100,草稿);
17 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10006,spring,理论,王超,2017-08-07 11:58:01,100,草稿);
18 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10007,scence,理论,王超,2017-08-07 11:58:01,100,草稿);
19 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10008,computer,理论,王超,2017-08-07 11:58:01,100,草稿);
20 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10009,math,理论,王超,2017-08-07 11:58:01,100,草稿);
21 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(100010,english,理论,王超,2017-08-07 11:58:01,100,草稿);
22 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10011,word,理论,王超,2017-08-07 11:58:01,100,草稿);
23 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10012,jsp,理论,王超,2017-08-07 11:58:01,100,草稿);
24 INSERT  into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(10013,CSS,理论,王超,2017-08-07 11:58:01,100,草稿);

 

38)PHP,获取数据库数据并在html中显示(晋级5)

标签:tab   .com   function   index   body   foreach   res   发布   isset   

原文地址:http://www.cnblogs.com/xiaoyoucai/p/7357042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!