查看用户道具
(1)在index.php页面,使用 PDO 方式,链接操作 MySQL 数据库。
(2)用内连接,连接查询用户有哪些道具并按用户id升序排序,用fetchALL()
方法将结果集中所有行的数组返回赋值给变量$result
(3)通过foreach方式遍历$result内数组,用<tr><td></td></tr>标签显
示内容,不需要加任何样式,所有拼接的结果都赋值给 $html。
例如:“<tr><td>张三</td><td>长矛</td><td>3.8</td></tr>”
(4)一次性echo输出变量$html
用户表[user]说明:
id 序号,
uname 用户名称,
upwd 用户密码,
用户道具表[user_equipment]说明:
id 序号,
userId 用户Id,
e_name 道具名称,
e_price 道具价格
e_num 道具数量
代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<style type="text/css">
section{
width:700px;
margin:0px auto;
border:1px solid #DEE1E6;
font-size:15px;
}
.title{
background-color:#009688;
font-weight:bold;
color:#FFF;
}
table{
border-collapse:collapse;
border-spacing:0;
width:100%;
}
td{
border-right:1px solid #DEE1E6;
border-bottom:1px solid #DEE1E6;
height:40px;
text-align:center;
width:80px;
}
tr:last-child{
border-style:none;
}
</style>
</head>
<body>
<section>
<table class="title">
<tr>
<td>姓名</td>
<td>道具名称</td>
<td>道具价格</td>
</tr>
</table>
<table>
<?php
header("content-type:text/html;charset=utf-8");
//$url,$user,$pwd是自动生成的数据库相关信息,不能修改
//连接数据库时不需要写端口号
$url = "mysql:host=mysql;dbname=database_25523_19_91533";//数据库ip和库名
$user = "25523_19_91533";//数据库用户
$pwd = "46650062c5b100547a13bd1b0b38c9bf";//数据库密码
$html = '';
$result="select * from user inner join user_equipment on order by id asc";
$r=$conn->query($result)->fetchALL();
$result= $r[0];
foreach ($result as $value){
$html="<tr><td>$user</td><td>$ename</td><td>$e_price</td></tr>";
}
echo $html;
$conn = new PDO($url,$user,$pwd);
?>
</table>
</section>
</body>
</html>
不知道代码哪里错了,请大佬指教!
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<style type="text/css">
section{
width:700px;
margin:0px auto;
border:1px solid #DEE1E6;
font-size:15px;
}
.title{
background-color:#009688;
font-weight:bold;
color:#FFF;
}
table{
border-collapse:collapse;
border-spacing:0;
width:100%;
}
td{
border-right:1px solid #DEE1E6;
border-bottom:1px solid #DEE1E6;
height:40px;
text-align:center;
width:80px;
}
tr:last-child{
border-style:none;
}
</style>
</head>
<body>
<section>
<table class="title">
<tr>
<td>姓名</td>
<td>道具名称</td>
<td>道具价格</td>
</tr>
</table>
<table>
<?php
header("content-type:text/html;charset=utf-8");
//$url,$user,$pwd是自动生成的数据库相关信息,不能修改
//连接数据库时不需要写端口号
$url = "mysql:host=mysql;dbname=database_25523_19_91533";//数据库ip和库名
$user = "25523_19_91533";//数据库用户
$pwd = "46650062c5b100547a13bd1b0b38c9bf";//数据库密码
$html = '';
$result="select * from user inner join user_equipment on user.id=user_equipment.userid order by user.id asc";
$conn=new PDO($url,$user,$pwd);
$result=$conn->query($result)->fetchALL();
foreach ($result as $value){
$html.="<tr><td>{$value["uname"]}</td><td>{$value["e_name"]}</td><td>{$value["e_price"]}</td></tr>";
}
echo $html;
?>
</table>
</section>
</body>
</html>