Jump to content

ORDER BY ASC DESC not working please help


surfwtw

Recommended Posts

I have my code listed below. This code is creating a list of links that will pull info from my database.  I am trying to have these listed from newest entires to oldest and for some reason it isn't working. Any help would be appreciated. Thanks

 

 

 

 

<?php

mysql_connect('host', 'db', 'pw') or die('Error connecting to MySQL');

mysql_select_db('db');

 

$url = 'http://www.website.info/articledisplay.php?id=';

 

 

$sqlCommand = "SELECT * FROM articles ORDER BY id DESC LIMIT 10";

// Execute the query here now

$query = mysql_query($sqlCommand) or die (mysql_error());

// Output the data here using a while loop, the loop will return all members

while ($row = mysql_fetch_array($query))

 

{

  $id = $row['id'] . " " . $title = $row['title'] . " " . $author = $row['author'];                           

 

  echo "<a href=\"{$url}{$id}{title}{author}\">{$id}</a><br />";

}

?>

Is your id column set as an integer?  If the column is set as a string type, then it orders by the first character.  So, all 1's (1,10,11,12, etc.) will come before the 2's (2,20,21,etc).

 

To test this out, make you a test table, with the following id's.  1,10,2,200,50,9,18.  Set the column type as varchar and run a query to order by the id desc, and asc.  Now change the column type to integer.

I got it worked out. I think I must have been working with a file that when I saved I made a typo in the filename so it was never getting uploaded to the server. God I feel stupid lol.  Thanks

it's the little things in programming that cause the most confusion

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.