36)PHP,获取数据库数据并在html中显示(晋级3)
首先展示我的html代码和php文件的位置关系:
然后我的php文件:
<?php
class db
{
public $host ;//= "localhost";//定义默认连接方式
public $User;//= "root";//定义默认用户名
public $Pwd;//= "root";//定义默认的密码
public $Dbname ;//= "thkphp5";//定义默认的数据库名
public $my_sql;
public $link;
public $result;
/*
* 构造函数
* 主机名,使用者,使用者密码,数据库的名字,查询语句
*/
public function __construct($config) {
$this->host=$config['host'];
$this->User=$config['user'];
$this->Pwd=$config['pwd'];
$this->Dbname=$config['dbname'];
$this->my_sql=$config['sql'];
$this->link= $this->connect();
$this->result= $this->Query($this->my_sql);
} //成员方法 是用来执行sql语句的方法
/*
* 数据库查询函数
* $sql string 是你的查询语句
*/
public function Query($sql)
//两个参数:sql语句,判断返回1查询或是增删改的返回
{
$db = $this->connect();
$r = $db->query($sql);
if (isset($r)) {
return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
} else {
return "数据库查询失败!";
} }
/*
* 数据库连接函数
*/
public function connect(){
$Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
return $Link;
} }
$sql='select * from zixun;';
$config=array('host'=>"localhost",'user'=>"root",'pwd'=>"root",'dbname'=>"thkphp5",'sql'=>$sql);
$shujuku=new db($config); include './login.html';
//var_dump($shujuku->result); ?>
然后我的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[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
<td><?php echo $row[];?></td>
</tr>
<?php endForeach;?>
</table>
</body>
</html>
然后我的结果展示:
改进的部分是:就是把之前的在__construct()函数中传值,一个一个的对应传入,变成了,我的数组$config,这样传入数据,其实,我还是可以在弄一个配置文件的,然后载入配置文件,将结果输入到我的主php文件的__construct()函数中。
数据库代码展示:
CREATE DATABASE `thkphp5` ;
use thkphp5 ;
create table zixun(
ZX_id int not null auto_increment primary key comment '咨询ID号',
ZX_name VARCHAR() NOT NULL COMMENT '咨询标题',
ZX_fenlei varchar() not null comment '资讯分类',
ZX_zuozhe varchar() not null comment '资讯作者',
gengxin_time DATETIME NOT NULL DEFAULT '2016-01-01 01:01:01' COMMENT '更新时间',
liulan_cishu int NOT NULL COMMENT '浏览次数',
fabu_zhuangtai VARCHAR() NOT NULL COMMENT '发布状态'
)engine=MyISAM charset=utf8;
INSERT into zixun(ZX_id, ZX_name, ZX_fenlei, ZX_zuozhe, gengxin_time, liulan_cishu, fabu_zhuangtai) values(, 'PHP', '理论', '王超', '2017-08-07 11:58:01', , '草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'C语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'JAVA语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'Mysql语言','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'html','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'spring','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'scence','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'computer','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'math','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'english','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'word','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'jsp','理论','王超','2017-08-07 11:58:01',,'草稿');
INSERT into zixun(ZX_id,ZX_name,ZX_fenlei,ZX_zuozhe,gengxin_time,liulan_cishu,fabu_zhuangtai) values(,'CSS','理论','王超','2017-08-07 11:58:01',,'草稿');
36)PHP,获取数据库数据并在html中显示(晋级3)的更多相关文章
- 38)PHP,获取数据库数据并在html中显示(晋级5)
还有一个加了单例模式的,在第52个. 首先是我的文件关系: 我的主php文件是index.php,我的配置文件php是BBB.php 我的数据库操作文件是 b.php 我的html文件是lo ...
- 37)PHP,获取数据库数据并在html中显示(晋级4)
我的php文件和html文件的位置关系: 然后我的主php文件是b.php,我的那个配置文件是BBB.php,我的html文件是login.html 然后我的b.php代码展示: <?php c ...
- 37)PHP,获取数据库值并在html中显示(晋级2)
下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了. 之前的那个变量的代码样子: cla ...
- jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用
JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...
- jmeter JDBC Request (查询数据库获取数据库数据) 的使用
JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...
- 转jmeter 性能测试 JDBC Request (查询数据库获取数据库数据) 的使用
JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...
- Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据
目录 前言 SpringBoot提供后端接口 Entity类 JPA操作接口 配置文件 数据库表自动映射,添加数据 写提供数据的接口 跨域问题 前端修改 效果图 待续 前言 Vue学习笔记九的列表案例 ...
- java获取数据库数据表的元数据
Connction conn; DatabaseMetaData dmd=conn.getMetaData();//获取数据库元数据 PreparedStatment ps; ps.getParame ...
- JaveWeb 公司项目(3)----- 通过Thrift端口获取数据库数据
前面两篇博客的内容主要是界面搭建的过程,随着界面搭建工作的完成,网页端需要加入数据,原先的B/S架构中C#通过Thrift接口获取数据,所以在网页端也沿用这个设计 首先,新建一个Maven下的Web项 ...
随机推荐
- @EnableAutoConfiguration激活自动装配
给予上个例子,将WebConfiguration类上的@SpringBootApplication换成@EnableAutoConfiguration.启动并运行http://localhost:80 ...
- golang实现单链表
package main import "fmt" type Object interface{} type Node struct { data Object next *Nod ...
- SpingBoot项目搭建(详细)
SpingBoot (原创:黑小子-余) springboot官网:->点击<- spring官网:->点击<- 一.SpringBoot简介 Spring Boot是由Piv ...
- 7.3 使用while 循环来处理列表和字典
# 7.3.1 在列表之间移动元素 # confirmed_users.py # 首先,创建一个待验证用户列表 # 和一个用于存储已验证用户的空列表 uncomfirmed_users = ['jam ...
- delphi 文本 记录 流式 读写文件
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- github访问过慢解决
为了更加愉快地使用全球最大同性交友网站上的优质资源,我们来做一些简单的本机上的调整. 通过查看下载链接,能够发现最终被指向到Amazon的服务器(http://github-cloud.s3.amaz ...
- (递归)P1036 选数
#include<stdio.h>#include<math.h>int x[20],n,k,i; //判断是否质数 int isprime(int n){ for(i= ...
- Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:2,栏位数:1
执行mybaits sql <delete id="delete4BatchesByLineCi" parameterType="java.util.List&qu ...
- 苹果智能AR挡风玻璃靠谱吗?
在过去十年,外界给苹果的形象一直是"伟大的硬件公司",他们的产品在外观方面往往比内涵更加引人注目,兼具娱乐性和艺术性, iPhone/iPad/iPod莫不如此,所以,当坊间传闻苹 ...
- Lambder笔记
记录Lambda语法(λ ,匿名函数)以及三个Python常见内置函数 形如:y=f(x)=x*x 使用lambda语法将对一个变量的运算抽象出来,如同f(),或是数学中的函数.关系.映射 f = l ...