下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了。

之前的那个变量的代码样子:

 class db
{
public $host = "localhost";//定义默认连接方式
public $User= "root";//定义默认用户名
public $Pwd= "root";//定义默认的密码
public $Dbname = "thkphp5";//定义默认的数据库名
public $my_sql;
public $link;
public $result;
public function __construct($host,$user,$pwd,$dbname,$sql) {
$this->host=$host;
$this->zhang=$user;
$this->mi=$pwd;
$this->dbname=$dbname;
$this->my_sql=$sql;
$this->link= $this->connect();
$this->result= $this->Query($this->my_sql);
}

看我的加粗部分,你看到了嘛,明明对应错了,所以啊,怪不得之后的代码不买账,你说你变量的名字都对应错了,人家再是小三语言,也会报错啊,所以啊,认真点

改进后的那段代码:

 class db
{
public $host ;//= "localhost";//定义默认连接方式
public $User;//= "root";//定义默认用户名
public $Pwd;//= "root";//定义默认的密码
public $Dbname ;//= "thkphp5";//定义默认的数据库名
public $my_sql;
public $link;
public $result;
public function __construct($host,$user,$pwd,$dbname,$sql) {
$this->host=$host;
$this->User=$user;
$this->Pwd=$pwd;
$this->Dbname=$dbname;
$this->my_sql=$sql;
$this->link= $this->connect();
$this->result= $this->Query($this->my_sql);
}

现在看到区别了吧,现在编译也对了,没有错误了

总的代码展示:

 <?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($host,$user,$pwd,$dbname,$sql) {
$this->host=$host;
$this->User=$user;
$this->Pwd=$pwd;
$this->Dbname=$dbname;
$this->my_sql=$sql;
$this->link= $this->connect();
$this->result= $this->Query($this->my_sql);
} //成员方法 是用来执行sql语句的方法
public function Query($sql,$type=)
//两个参数:sql语句,判断返回1查询或是增删改的返回
{
//造一个连接对象,参数是上面的那四个
// $db = new mysqli($this->host,$this->zhang,$this->mi,$this->dbname);
$db=$this->connect();
$r = $db->query($sql);
if($type == "")
{
return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
}
else
{
return $r;
}
}
public function connect(){
$Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
return $Link;
} }
$sql='select * from zixun;';
$shujuku=new db("localhost","root","root","thkphp5",$sql); 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>

然后我的php文件和html文件的位置关系:

是那个b.php和login.html,不是BBB.php,那个图是上一个的,我没有重新弄,我太懒了。

然后就是结果展示:

数据库代码展示:

 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',,'草稿');

37)PHP,获取数据库值并在html中显示(晋级2)的更多相关文章

  1. 37)PHP,获取数据库数据并在html中显示(晋级4)

    我的php文件和html文件的位置关系: 然后我的主php文件是b.php,我的那个配置文件是BBB.php,我的html文件是login.html 然后我的b.php代码展示: <?php c ...

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

    还有一个加了单例模式的,在第52个. 首先是我的文件关系: 我的主php文件是index.php,我的配置文件php是BBB.php  我的数据库操作文件是  b.php    我的html文件是lo ...

  3. 36)PHP,获取数据库数据并在html中显示(晋级3)

    首先展示我的html代码和php文件的位置关系: 然后我的php文件: <?php class db { public $host ;//= "localhost";//定义 ...

  4. web自动化框架之三获取数据库值与界面值比较~~

    数据库用到的是mysql,框架涉及数据库,主要包含两个方面,一个是每个案例执行完毕后,插入案例相关信息与数据:一个是web界面数据核对的时候,需要从sql中获取某行某列值与界面某个值做比较. 描述:w ...

  5. 使用Kinect2.0获取点云以在GLUT中显示

    这篇文章用来记录Kinect2.0如何生成点云. 以下示例源自Kinect提供的example修改完成,其名称会在小标题下方注解. 首先,要获取点云需要获取图像的深度数据和颜色数据.最后再将深度数据与 ...

  6. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  7. JS中获取数据库中的值

    在本次项目中,遇到很多问题,经过努力,都逐步得到解决.静下心来,做一个记录,以供以后学习. 在项目中遇到一个问题,需要在JS中读取数据库中的值,然后再把值返回到页面中,解决方案如下:使用Ajax方法来 ...

  8. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态

    本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...

  9. 获取数据库表详细信息、存储过程、视图、的sql

    select s.[name] + '.' + t.[name] as tablename from sys.tables as t,sys.schemas as s where t.schema_i ...

随机推荐

  1. WebServerInitializedEvent &ApplicationRunner

    application.properties app.name=yaoyuan2 app.dept.id=1 MyConfig.java import lombok.AllArgsConstructo ...

  2. ArchLinux安装Gnome桌面

    给Arch安装Gnome桌面美化及常用软件配置 一.创建普通用户 1.安装zsh 个人比较喜欢的一个shell,你们可以和我不同 # pacman -S zsh 2.创建用户 kain是我创建用户的名 ...

  3. IDEA--IDEA配置web项目

    参考:https://blog.csdn.net/kfm1376822651/article/details/79666586 记学习springmvc时,使用idea部署web项目至tomcat. ...

  4. STM32速度---网页讲解

    1. ① USART串口,若最大波特率只需115.2k,那用2M的速度就够了,既省电也噪声小. ② I2C接口,若使用400k波特率,若想把余量留大些,可以选用10M的GPIO引脚速度. ③ SPI接 ...

  5. Windows下Nodejs的开发环境搭建

    我为什么选择学习 Node.js Node.js是前后端分离开发的新选择.比较热门的前端框架React,Vue都可以在Node.js环境上开发. Node.js 是一个基于 Chrome V8 引擎的 ...

  6. ZOJ- 2562 反素数使用

    借用了下东北师大ACM的反素数模版. 本来我是在刷线段树的,有一题碰到了反素数,所以学了一下..有反素数的存在,使得一个x ,使得x的约数个数,在1 到 x的所有数里面,是最大的. 这里面还涉及安叔那 ...

  7. JNI传递修改自定义Java Class数组数据

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 结合前面讲的2篇关于JNI的文章,这里直接把代码贴上,主要是要知道如果传递自定义Class Array的时 ...

  8. iOS 蓝牙开发详解

    目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另 ...

  9. 配置window下python3环境

    功能介绍 整理生信小知识库,一些技巧一些知识. 昨天 以下配置环境基于window操作系统,安装python3版本为例,推荐基础版配置. !   METHOD 1 (基础版) 官网下载对应电脑版本的p ...

  10. This inspection highlights chained comparisons that can be simplified.

    https://stackoverflow.com/questions/26502775/pycharm-simplify-chained-comparison In Python you can & ...