简体   繁体   中英

PHP and MySQL ORDER BY date_time error

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);.

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.

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 ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM