39)PHP,选取数据库中的两列

 

首先是我的文件关系:

我的b.php是主php文件,BBB.php是配置文件,login。html是显示文件,

b.php文件代码:

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

我的BBB.php文件:

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

我的login.html文件:

 1 <!-- 模板文件,利用HTML代码展示数据 -->
 2 <!DOCTYPE html>
 3 <html lang="en">
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>比赛列表</title>
 7 </head>
 8 <body>
 9 
10 <table>
11     <tr>
12         <th>ZX_id</th>
13         <th>ZX_name</th>
14         <!--<th>ZX_fenlei</th>-->
15         <!--<th>ZX_zuozhe</th>-->
16         <!--<th>更新时间</th>-->
17         <!--<th>浏览次数</th>-->
18         <!--<th>发布状态</th>-->
19     </tr>
20     <?php foreach($shujuku->result as $row) : ?>
21     <tr>
22         <td><?php echo $row[0];?></td>
23        <td><?php echo $row[1];?></td>
24         <!--<td><?php echo $row[2];?></td>-->
25         <!--<td><?php echo $row[3];?></td>-->
26         <!--<td><?php echo $row[4];?></td>-->
27         <!--<td><?php echo $row[5];?></td>-->
28         <!--<td><?php echo $row[6];?></td>-->
29     </tr>
30     <?php endForeach;?>
31 </table>
32 </body>
33 </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,'草稿');

 

posted @ 2017-08-14 12:28  小油菜1  阅读(420)  评论(0编辑  收藏  举报