php - 试图在 html 表中显示 SQL 数据

标签 php mysql

所以我有一个文件应该运行 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/

相关文章:

python - Django cooking 食谱站点模型结构

php - 具有一个或多个(多个)参数的搜索表单

php - 将sql查询转换为laravel5.2?

php - CREATE TABLE SQL 语句不会运行

php - 我无法使用 json 在本地主机服务器中发布数据和图像

mysql - 我可以在 mySQL 中以 "DD-Mon-YY"格式插入日期吗?

MySQL While循环抛出错误

php - 如何使用插入查询,其中一个值是子查询的结果

php - 在linux服务器上运行C程序

PHP 7 : Multiple function return types