所以我有一个文件应该运行 sql 查询并返回数据然后填充一个 html 表但是由于某种原因它没有返回数据,在数据库中的 sql 中查询确实返回数据但不返回数据我的网站。
<?php
//run the query
$sql = "SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email
FROM polling_results WHERE 'topic_id' = '147796'
ORDER BY 'id, displayorder'";
$result = mysql_query($sql);
//fetch the results
while ($row = mysql_fetch_array($result))
{
//display the results
echo '<br /><table class="table table-bordered table-condensed">';
echo '<thead><tr>';
echo '<th>Name</th>';
echo '<th>Email</th>';
echo '<th>Question Text</th>';
echo '<th>Answer</th>';
echo '</tr></thead>';
echo '<tbody><tr>';
echo "<td>".$row['first_name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['longdesc']."</td>";
echo "<td>".$row['text']."</td>";
echo '</tr></tbody></table>';
}
?>
成功了,感谢大家的帮助。
最佳答案
您要打开与数据库的连接吗?我建议使用 mysqli 而不是 mysql。
// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn)
{
die ( "Connection failed: " . mysqli_connect_error () );
}
此外,您应该将表创建移到 while 之外,因为这样它将为每一行创建一个新表。
echo '<br /><table class="table table-bordered table-condensed">';
echo '<thead><tr>';
echo '<th>Name</th>';
echo '<th>Email</th>';
echo '<th>Question Text</th>';
echo '<th>Answer</th>';
echo '</tr></thead>';
echo '<tbody>';
//display the results
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo "<td>".$row['first_name']."</td>";
echo "<td>".$row['email']."</td>"
echo "<td>".$row['longdesc']."</td>";
echo "<td>".$row['text']."</td>";
echo '</tr>';
}
echo '</tbody></table>';
关于php - 试图在 html 表中显示 SQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365160/