2012-09-17 39 views
0

我試着通過他們的最新訂購的職位,但每當我嘗試這樣做,我得到這個錯誤:無法排序包含日期

警告:mysql_num_rows()預計參數1是資源,布爾在C中給出: \本地主機\引導\ category.php在線58

數據庫結構:http://puu.sh/1630b

<?php 
//category.php 
include 'connect.php'; 

//first select the category based on $_GET['cat_id'] 
$sql = "SELECT 
      cat_id, 
      cat_name, 
      cat_description 
     FROM 
      categories 
     WHERE 
      cat_id = " . mysql_real_escape_string($_GET['id']); 

$result = mysql_query($sql); 

if(!$result) 
{ 
    echo 'The category could not be displayed, please try again later.' . mysql_error(); 
} 
else 
{ 
    if(mysql_num_rows($result) == 0) 
    { 
     echo 'This category does not exist.'; 
    } 
    else 
    { 
     //display category data 
     while($row = mysql_fetch_assoc($result)) 
     { 
      echo '<h2>Topics in &prime;' . $row['cat_name'] . '&prime; category</h2><br />'; 
      $title = $row['cat_name']; 
         include 'header.php'; 
     } 

     //do a query for the topics 
     $sql = "SELECT 
        topic_id, 
        topic_subject, 
        topic_date, 
        topic_cat 
       FROM 
        topics 
       ORDER BY 
        topic_date DESC 
       WHERE 
        topic_cat = " . mysql_real_escape_string($_GET['id']); 

     $result = mysql_query($sql); 

//  if(!$result) 
//  { 
//   echo 'The topics could not be displayed, please try again later.'; 
//  } 
//  else 
//  { 
      if(mysql_num_rows($result) == 0) 
      { 
       echo 'There are no topics in this category yet.'; 
      } 
      else 
      { 
       //prepare the table 
       echo '<table border="1" class="table table-bordered table-striped" style="float: right; width: 990px;"> 
         <tr> 
         <th>Topic</th> 
         <th>Created at</th> 
         </tr>'; 

       while($row = mysql_fetch_assoc($result)) 
       {    
        echo '<tr>'; 
         echo '<td class="leftpart">'; 
          echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h3>'; 
         echo '</td>'; 
         echo '<td class="rightpart">'; 
          echo date('d-m-Y', strtotime($row['topic_date'])); 
         echo '</td>'; 
        echo '</tr>'; 
        echo ''; 
        echo ''; 
       } 
       echo '</table>'; 
       echo '</div>'; 
      } 
    // } 
    } 
} 

include('footer.php'); 
?> 
+0

那麼你的日期列是什麼數據類型?並學習使用SO編輯器中的魔法按鈕 –

+2

'ORDER BY'在WHERE子句之後。 –

+0

我已經把它移到WHERE後面,但是我得到了同樣的錯誤,我的數據類型是datetime – Lewes

回答

0

您的查詢執行,失敗,並返回一個布爾值,而不是資源!

  • 建立在腳本中的一些錯誤處理。
  • 不使用mysql_函數,它們已被棄用。

現在你已經編輯了你的文章,很顯然ORDER BY之後WHERE

+0

對,對不起,說實話,我實在不知道你剛剛告訴我什麼。 – Lewes

+0

在$ result = mysql_query($ sql)或die(mysql_error())中更改$ result = mysql_query($ sql); – JvdBerg

+0

做到了這一點,並得到這個錯誤:解析錯誤:語法錯誤,意想不到的T_VARIABLE在C:\ localhost \ bootstrap \ category.php在39行 – Lewes

1

我這種情況下,問題將出現在第52-57行,它們應該檢查mysql_query是否成功。您的查詢失敗並返回false(布爾值),這是一個有效的返回值。

錯誤本身取決於您的數據庫表結構(不是鏈接的一部分)。