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条)

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答