Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.