2

I simply want to ORDER the comments by the ID, but I have no luck in doing it. Can't figure out what to do, because this is confusing me: articleid='" . mysql_real_escape_string($_GET['id']) . "'

Would you guys happen to know how I could go about ordering the comments by the id in DESC? thanks!

<?php

$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); $comments = mysql_num_rows($amount_get);

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'");


if (mysql_num_rows($grab)==0) {

    echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}

    while($row = mysql_fetch_array($grab)){

    ?>
Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
TrippedStackers
  • 427
  • 1
  • 4
  • 17

7 Answers7

8

First of all you're doing the same SELECT two times. That's pretty unnecessary since you can count rows and get the data from a single query. Additionally to this replace commentid with the unique id of your comment table and you're set. Replace DESC with ASC to reverse the sort order.

<?php
    $grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY commentid DESC");
    $comments = mysql_num_rows($grab);

    if (mysql_num_rows($grab)==0) {
        echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
    }

    while($row = mysql_fetch_array($grab)){

?>
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • See I tried that before, but it didn't work. Probably because as you said, I had SELECT 2 times. But now it works perfectly. Thank you! and to everyone else who helped.. I appreciate it! – TrippedStackers Nov 29 '12 at 07:52
1

add ORDER BY clause

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY articleid, ID DESC");

your query is vulnerable with SQL Injection, please read the article below to protect from it,

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

try this

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' order by articleid desc");
Vamsi
  • 873
  • 1
  • 8
  • 16
0

I think your comments table habe a column id then, so:

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");

This is a sql thing to sort it, not php, so you just have to modify your sql statement.

YvesR
  • 5,922
  • 6
  • 43
  • 70
0
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC "); 

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");
som
  • 4,650
  • 2
  • 21
  • 36
0

Three suggestions:

1) Complete your "select" statement in a string variable (as shown below; it makes debugging much easier)

2) Consider using prepared statements instead of raw "select" (or update or delete!).

It can help performance. But it makes your PHP much more secure!

3) Consider moving away from the (deprecated) mysql_query() syntax

<?php

  $sql = 
    "SELECT * FROM comment WHERE articleid='" . 
    mysql_real_escape_string($_GET['id']) . "' order by articleid desc";
  $amount_get = mysql_query($sql); 
  $comments = mysql_num_rows($amount_get);

  $sql = 
    "SELECT * FROM comment WHERE articleid='" . 
    mysql_real_escape_string($_GET['id']) . "'order by articleid desc";
  $grab = mysql_query($sql);
  ...

Here's a good link on the mySQLi and PDI APIs that supercede the old mysql_query() syntax:

And here's a good link on prepared statements:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

You can use order by clause in your query

    <?php
$getarticles = array();
$getarticles = mysql_query("SELECT * FROM comment order by articleid desc");

if(empty($getarticles)){
echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}
echo "<pre>";
print_r($getarticles);
echo "</pre>";

    for($i=0;$i<count($getarticles);$i++){
//display
}

    ?>
Avinesh Kumar
  • 1,389
  • 2
  • 17
  • 26