试图在html表中显示SQL数据

时间:2017-07-28 04:57:05

标签: 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>';
            }
    ?>

得到它的工作,谢谢所有的帮助家伙/女孩。

4 个答案:

答案 0 :(得分:1)

您是否正在打开与DB的连接?我建议使用mysqli而不是mysql。

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>';

此外,您应该将表创建移动到您的while之外,这样它将为每一行创建一个新表。

UPDATE table t1 set t1.fieldA = 0 
where t1.TIMESTAMP = (select max(t2.TIMESTAMP) from table t2 where t2.id = t1.id  )
and id in  (1111,2222,33333)

答案 1 :(得分:0)

你试图回显$ sql并尝试数据是否存在..如果有效,请尝试回显$ result ..

答案 2 :(得分:0)

从查询'topic_id'ORDER BY 'id, displayorder'

中删除引号

您的查询:

$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'"

编辑查询:

$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"

答案 3 :(得分:0)

您需要更改以下代码: 因为mysql_ *从PHP 5.6及以后不推荐使用,所以你应该使用mysqli_ *。

按如下方式创建数据库连接:

    <?php
    // Create connection
    $db_conn = mysqli_connect ( $servername, $username, $password, $dbname );
    // Check connection
    if (! $db_conn)
    {
        die ( "Connection failed: " . mysqli_connect_error () );
    }
?>

现在你的代码:

<?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 = mysqli_query($db_conn,$sql);

            // Creating table format

             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>';


            //fetch the results
            while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                //display the results
               <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>';
            // display data end.
    ?>

如果您遇到任何问题,请告诉我。