php - 无法按日期排序

标签 php mysql

我试图按日期对帖子进行排序,但每当我尝试这样做时,我都会收到此错误:

警告:mysql_num_rows() 期望参数 1 为资源, bool 值在第 58 行 C:\localhost\bootstrap\category.php 中给出

数据库结构: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');
?>

最佳答案

在这种情况下,问题将出现在注释行 52 - 57 中,这些行应该检查 mysql_query 是否成功。您的查询失败并返回 false( bool 值),这是一个有效的返回值。

错误本身取决于您的数据库表结构(不是链接的一部分)。

关于php - 无法按日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464304/

相关文章:

php - foreach 不填充数组

java - GenericHibernateDAO 的方法 findById 中的 "lock" boolean 值是什么意思?

MySql 搜索两张表,一张表只做计数

php - 连接数据库错误类型 : 2002: Permission denied

javascript - 使用 .trigger() 模拟多个按键;

php - 根据时间戳检查 DB int 字段

mysql - SQL:从列表中提取项目并将其放入新行

mysql - 你能将字符异常编程到 MySQL 的数字列中吗?

javascript - 从URL读取网页内容

php - 测试递归方法