Connecting Tech Pros Worldwide Forums | Help | Site Map

ORDER BY variable DESC

Newbie
 
Join Date: May 2009
Posts: 21
#1: Jun 17 '09
Hello,

I would like the code below to sort the echoed output by votes_up descending. I have added "ORDER BY votes_up DESC" to both queries, but the echoed output is still not sorting. Any ideas why it is not working?

Thanks in advance,

John

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SHOW TABLES FROM feather") 
  2. or die(mysql_error()); 
  3.  
  4. while(list($table)= mysql_fetch_row($result))
  5. {
  6.   $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry' ORDER BY votes_up DESC";
  7.   $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
  8.   list($isThere) = mysql_fetch_row($resA);
  9.   if ($isThere)
  10.   {
  11.      $table_list[] = $table;
  12.   }
  13. }
  14.  
  15. foreach ($table_list as $table) { 
  16.     $sql = "SELECT votes_up FROM `$table` WHERE `site` LIKE '$entry' ORDER BY votes_up DESC"; 
  17.     $sql1 = mysql_query($sql) or die("$sql:".mysql_error());
  18.    while ($row = mysql_fetch_assoc($sql1)) {
  19.        echo $table . ': "' . $row['votes_up'] . " for $entry from $table\"<br />";
  20.    } 
  21. }

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Jun 17 '09

re: ORDER BY variable DESC


you need to troubleshoot when you're code is not working and is not producing an error that we could understand.

why don't you echo your $sql variables, take those and plug them into a mysql client, run the query manually to see if the output is even what you're looking for.



Dan
Newbie
 
Join Date: Jun 2009
Posts: 2
#3: Jun 19 '09

re: ORDER BY variable DESC


so confused, i dont know what u r going to do...

if u wanto sort the result, u should code like this:

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * 
  2.         FROM `table_name` 
  3.         WHERE `id` = '".$id."' 
  4.         ORDER BY `timestamp` DESC  ";
  5. $query = mysql_query($sql);
  6. while ($rt = mysql_fetch_assoc($query)){
  7.     echo '<pre>';
  8.     print_r ($rt);
  9.     echo '</pre>';
  10. }
  11.  
Reply