Lewes
Lewes

Reputation: 123

Unable to sort with date

Im trying to order posts by their date, but whenever I try to do that I get this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\localhost\bootstrap\category.php on line 58

DATABASE STRUCTURE: 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');
?>

Upvotes: 0

Views: 120

Answers (2)

Jakub Novotny
Jakub Novotny

Reputation: 36

I this case the problem will be in the commented lines 52 - 57 which are supposed to check if the mysql_query has been successful. Your query fails and returns false (boolean), which is a valid return value.

The error itself depends on your database table structure (isn't part of your link).

Upvotes: 1

JvdBerg
JvdBerg

Reputation: 21866

Your query that executes, fails and returned a boolean instead of a resource!

  • build in some error handling in your script.
  • do not use mysql_ functions, they are deprecated.

And now that you have edited your post, it is obvious that the ORDER BY comes after the WHERE.

Upvotes: 0

Related Questions