Sorting Records In A Table

I can not get the following code to sort the data

The output of this code will not sort for some reason?
I can't see the error.


// CONNECT TO DATABASE
mysql_connect           ($dbhost,$dbuser,$dbpass);
@mysql_select_db        ($db);


// INSERT
if ($URLBOX)
{
# DISABLED IN THIS LIST SCRIPT
#  $URLBOX=str_replace('http://','',$URLBOX);
#  $sql = "INSERT INTO $table (URL, DESCRIPTION, CITY) VALUES('$URLBOX', '$DESCRIPTIONBOX', '$CITY')";
#  $sql_result = mysql_query($sql) or die("Couldn't execute query.");
}

$result = mysql_query("SELECT * FROM $table LIMIT 0,30 ORDER BY country ASC");
$row_cnt = 0;

$qid = mysql_query("SELECT country, url, description, city, telephone FROM $table");
?>

<table width="100%" border="1">
   <tr>
    <td>COUNTRY</td>
    <td>CITY</td>
    <td>DESCRIPTION</td>
    <td>URL</td>
    <td>TELEPHONE</td>
</tr>
<? while ($obj = mysql_fetch_object($qid)) { ?>
  <tr>
    <td><?=$obj->country?></td>
    <td><?=$obj->city?></td>
    <td><?=$obj->description?></td>
    <td><?=$obj->url?></td>
    <td><?=$obj->telephone?></td>
  </tr>
<? } ?>
</table>

<?
mysql_close();
?>

Output can be viewed at http://www.hipbase.com/linklist/list.php

Thanks
mrlayanceAsked:
Who is Participating?
 
bibana2Connect With a Mentor Commented:


I might be reading this wrong but I think the reason it's not sorting is because there is no "ORDER BY" in the following piece of code:

$qid = mysql_query("SELECT country, url, description, city, telephone FROM $table");


As far as I can tell, it's just going to spit out the results in the order the d-base finds them.

I hope that helps?
0
 
carchitectCommented:
$result = mysql_query("SELECT * FROM $table LIMIT 0,30 ORDER BY country ASC");
$row_cnt = 0;

$qid = mysql_query("SELECT country, url, description, city, telephone FROM $table");

bibana is correct and put limit clause after order by clause

$result = mysql_query("SELECT * FROM $table ORDER BY country ASC LIMIT 0,30");

regards

0
 
laurlyCommented:
you forgot to add the order by in your second sql statment which is the one that you are using in your while loop.


$qid = mysql_query("SELECT country, url, description, city, telephone FROM $table order by country asc");


laurly
0
All Courses

From novice to tech pro — start learning today.