Connecting Tech Pros Worldwide Help | Site Map

ORDER BY variable DESC

  #1  
Old June 17th, 2009, 05:52 AM
Newbie
 
Join Date: May 2009
Posts: 21
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. }
  #2  
Old June 17th, 2009, 07:06 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

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
  #3  
Old June 19th, 2009, 10:07 AM
Newbie
 
Join Date: Jun 2009
Posts: 2

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA SQL query resets variable values to "0" KelvinA answers 8 July 29th, 2007 11:53 PM
Using Variables in Order By Clause Kamran76 answers 1 January 9th, 2007 07:47 PM
order by with variable chadsmith76@gmail.com answers 5 June 8th, 2006 09:05 PM
does DB2 support SELECT - INTO statement with ORDER BY clause? GreatAlterEgo answers 26 January 31st, 2006 07:05 PM