2017-03-15 27 views
-2

警告:mysql_num_rows()預計參數1是資源,布爾在C中給出:\ XAMPP \ htdocs中\ newsite上線\ view_category.php 117php參數問題,最新錯誤?

我知道它'if(mysql_num_rows($res2) > 0)',但它應該是真正的0 ,如果不是,我下一步該做什麼?

這裏的代碼

$cid = $_GET['cid']; 
 
\t \t if (isset($_SESSION['uid'])){ 
 
\t \t \t $logged = " | <a href='create_topic.php?cid=".$cid."'>Click Here To Create A Topic</a>"; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t $logged = " | Please log in to create topics in this forum."; 
 
\t \t 
 
\t \t } 
 
\t \t $sql = "SELECT id FROM categories WHERE id='".$cid."' LIMIT 1"; 
 
\t \t $res = mysql_query($sql) or die(mysql_error()); 
 
\t \t if (mysql_num_rows($res) == 1) { 
 
\t \t \t $sql2= "SELECT * FROM topics WHERE category_id='".$cid."' ORDER BY topic_reply_date DESC"; 
 
\t \t \t $res2= (mysql_query($sql2) or die(mysql_error())); 
 
\t \t \t if(mysql_num_rows($res2) > 0) { 
 
\t \t \t \t $topics .= "<table width='100%' style='border-collapse: collapse;'>"; 
 
\t \t \t \t $topics .= "<tr><td colspan='3'><a href='thread.php'>RETURN TO FORUM INDEX</a>".$logged."<hr /></td></tr> "; 
 
\t \t \t \t $topics .= "<tr style='background-color: #dddddd;'><td>TOPIC TITLE</td><td width='65' align='center'>REPLIES</td><td width='65' align='center'>VIEWS</td></tr>"; 
 
\t \t \t \t $topics .= "<tr><td colspan = '3'><hr /></td><tr>"; 
 
\t \t \t \t while ($row = mysql_fetch_assoc($res2)){ 
 
\t \t \t \t \t $tid = $row['id']; 
 
\t \t \t \t \t $title = $row['topic_title']; 
 
\t \t \t \t \t $views = $row['topic_views']; 
 
\t \t \t \t \t $date = $row['topic_date']; 
 
\t \t \t \t \t $creator = $row['topic_creator']; 
 
\t \t \t \t \t $topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><hr /><span class ='post_info'>Posted by: ".$creator." on ".$date."</span></td><td align='center'>0</td><td align='center'>".$views."</td></tr>"; 
 
\t \t \t \t \t $topics .= "<tr><td colspan = '3'><hr /></td></tr>"; 
 
\t \t \t \t \t 
 
\t \t \t \t } 
 
\t \t \t \t $topics .= "</table>"; 
 
\t \t \t }else{ 
 
\t \t \t \t echo "<a href='thread.php'>Return to Forum Index</a><hr/>"; 
 
\t \t \t \t echo "<p> There are no topics in this category yet.</p>"; 
 
\t \t \t } 
 
\t \t } 
 
\t \t \t else{ 
 
\t \t \t \t echo "<a href='form.php'>Return to Forum Index</a><hr/>"; 
 
\t \t \t \t echo "<p>You ar trying to view an unexisted category"; 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t } \t 
 
\t \t ?>

太感謝你了

+0

此錯誤意味着您正試圖獲得對一個查詢選出的記錄數,但該查詢並沒有因錯誤成功地完成。所以你必須先解決查詢。順便說一句,您正在使用已在PHP7中刪除的棄用API(mysql_ *)。切換到mysqli_ *或PDO,並學習avout準備語句,以防止sql注入 –

+0

此代碼通過[小Bobby表](https://xkcd.com/327/)批准。 – bishop

回答

0

需要注意的是PHP的布爾運算符總是返回一個布爾值...而不是返回其他語言最後評估的表達式的值,所以(mysql_query($sql2) or die(mysql_error()))返回一個布爾值。同樣,你不能使用'||'操作員設置默認值。相反,您必須使用'?:'運算符。 在這種情況下,你可以使用:

$res2 = mysql_query($sql); 
if(!$res2) { 
    die(mysql_error()); 
}