下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__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. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

  2. windows FTP上传

    TCHAR tcFileName[MAX_PATH * 4] = {L"visio2010永久安装密钥.txt"}; TCHAR tcName[MAX_PATH * 4] = {0 ...

  3. shell字典使用

    1.shell界面或脚本中 pcode=(["10"]="BeiJing" ["22"]="TianJin") echo ...

  4. dmidecode 硬件

    dmidecode -t Memory   #查内存 Maximum Capacity: 4 GB  #最大4G         Number Of Devices: 2   #2个插槽 服务器到底能 ...

  5. ci框架与smarty的整合

    ci框架与smarty的整合 来源:未知    时间:2014-10-20 11:38   阅读数:108   作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...

  6. CPU构架和工作原理

    -- CPU -- -- CPU 由三部分组成:时钟:控制单元:算术逻辑单元 -- -- -- 时钟:对CPU内部操作与系统其他硬件进行同步: -- -- -- 控制单元:控制机器指令的执行顺序: - ...

  7. Django1.11模型类数据库操作

    django模型类数据库操作 数据库操作 添加数据 1,创建类对象,属性赋值添加 book= BookInfo(name='jack',pub_date='2010-1-1') book.save() ...

  8. UML-用例关联

    1.用例关联:就是各个用例之间的关系,分3种关系分别是:包含关系.扩展关系.泛化关系. 2.包含关系 1).示例 2).使用场景 A.用例在其他用例中重复使用 B.用例非常复杂冗长,将其分解为子单元便 ...

  9. vue项目起步准备

    1. 项目环境: node.js运行环境(不一定要最新特性的最新版本,用合适的版本即可) 2.项目放在git上管理(网上云仓库码云) 1.创建仓库:选择语言js 2.本地代码和线上代码通过git做成关 ...

  10. GUI(Graphical User Interface)

    译:用户和图形界面 GUI与程序交互的不同方式,包含3基本要素:输入,处理和输出. 常用GUI框架包括以下几种: wxPython Kivy Flexx PyQt Tkinter Pywin32 Py ...