Reputation: 546
I am developing my own forums and everything is working perfectly apart from when I attempt to ORDER BY date_time. My table name is correct and so is all the field names! A PHP error is given when I add ORDER BY date_time to the query and I can't understand why, as it works perfectly fine elsewhere on the site. The code and error are below:
// query responses
$sqlresponses = mysql_query("SELECT * FROM forum_replb ORDER BY date_time WHERE post_id='$disc_id'");
$responseList = "";
$numRows = mysql_num_rows($sqlresponses);
if ($numRows < 1) {
$responseList = "There are currently no responses to this discussion/post yet! Add one above.";
} else {
while($row = mysql_fetch_array($sqlresponses)){
$response_author_id = $row["author_id"];
$reply_body = $row["reply_body"];
$date_time = $row["date_time"];
$date_time = strftime("%b %d, %Y", strtotime($date_time));
$responseList .= '<div id="sub_response_module"><p>' . $reply_body . ' -
<a href="../profile.php?id=' . $response_author_id . '">' . $response_author_id . '</a>
| <i>' . $date_time . '</i></p></div>';
}
}
Error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/public_html/lb/forum/post.php on line 49
Just to clarify, line 49 is $numRows = mysql_num_rows($sqlresponses);.
Upvotes: 0
Views: 162
Reputation: 6116
Your ORDER BY clause needs to be after the WHERE clause:
SELECT * FROM forum_replb WHERE post_id='$disc_id' ORDER BY date_time
Please also note that the mysql_ family of functions are deprecated. You should use the mysqli_ functions, or possibly better still PDO.
Upvotes: 2
Reputation: 2651
You have a syntax error in your query - you should put the ORDER BY after the WHERE.
mysql_query("SELECT * FROM forum_replb WHERE post_id='$disc_id' ORDER BY date_time ");
Upvotes: 2