2017-07-28 45 views
1

所以我有一個文件,應該運行一個SQL查詢並返回數據,然後填充一個HTML表,但由於某種原因,它不返回數據庫中的數據該查詢確實會返回數據,但不在我的網站上。試圖在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>'; 
      } 
    ?> 

得到它的工作,感謝所有幫助傢伙/女孩。

+0

有沒有mysql的錯誤?試試var_dump()對行變量,也不要用mysql使用mysqli來代替。 –

+2

從'ORDER BY'id,displayorder''刪除單引號,以便它的'ORDER BY id,displayorder' –

+0

var_dump()甚至沒有輸出,名稱等等,幾乎就像它不會輸出進入while語句 –

回答

0

您需要更改類似下面的代碼: 由於mysql_的*從PHP 5.6棄用,以後所以你應該使用mysqli_ *。

創建DB連接如下:

<?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. 
    ?> 

如果您遇到任何問題,請讓我知道。

0

嘗試呼應$ SQL和嘗試的數據是有或不..如果它的工作原理嘗試呼應$結果..

+0

當我回聲$ sql;它只是打印出SELECT ID,topic_id,name,surveyid,questionid,longdesc,text,first_name,last_name,email FROM vw_polling_results WHERE'topic_id'='147796'ORDER BY id,displayorder –

+0

您嘗試將查詢更改爲topic_id =' 147796'和id,displayorder ..刪除引號並嘗試.. – Jok3r

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

你打開一個到數據庫的連接嗎?我建議使用mysqli而不是mysql。

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

此外,您應該將表創建移動到您的時間之外,因爲這樣它會爲每一行創建一個新表。

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