doujie4344 2018-01-28 17:46
浏览 49
已采纳

用html和php拉数据

I'm back again with another, probably simple question. Why exactly is this not working? It connects to the dBase, the html table shows, but no data is showing. It's only displaying $row in each cell and some info about the if statement. I've attempted a few different variations, and in the end, every method I have attempted to utilize to pull information from the dBase has resulted in the same problem. It looked simple enough to me, but here were are.

<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
<style>
table {
border-collapse: collapse;
width: 100%;
color: #588c7e;
font-family: monospace;
font-size: 25px;
text-align: left;
 } 
th {
background-color: #588c7e;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>

<table>
<tr>
<th>Id</th> 
<th>First Name</th> 
<th>Last Name</th>
<th>Address</th> 
<th>City</th> 
<th>State</th>
<th>Zipcode</th> 
<th>Telephone</th> 
<th>Email</th>
</tr>

<?php
$con = mysqli_connect('localhost','username','password');

if(!con) {
echo 'Error: Not connected to the server.';
}

if(!mysqli_select_db($con,'MPITdBase')) {
echo 'Error: Database is not selected';
}

$sql = "SELECT  `custID` ,  `custLastName` ,  `custFirstName` ,  
`custStreet` ,  
`custCity` ,  `custState` ,  `custZipcode` ,  `custTX` ,  `custEmail` 
FROM  `Customer`";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>"$row["custID"]"</td><td>"$row["custFirstName"]"</td>
<td>"$row["custLastName"]"</td><td>"$row["custStreet"]"</td>
<td>"$row["custCity"]"</td><td>"$row["custState"]"</td>
<td>"$row["custZipcode"]"
</td><td>"$row["custTX"]"</td><td>"$row["custEmail"]"</td></tr>";
}
echo "</table>";
} else { echo "0 results"; } 
$conn->close();
?>
</table>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • duanhuan3012 2018-01-28 17:51
    关注

    You have to use the . (dot) operator to concat strings

    while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["custID"]."</td><td>".$row["custFirstName"]."</td>
    <td>".$row["custLastName"]."</td><td>".$row["custStreet"]."</td>
    <td>".$row["custCity"]."</td><td>".$row["custState"]."</td>
    <td>".$row["custZipcode"]."
    </td><td>".$row["custTX"]."</td><td>".$row["custEmail"]."</td></tr>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP