?
Solved

Sorting Records In A Table

Posted on 2003-02-26
5
Medium Priority
?
238 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:mrlayance
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
bibana2 earned 280 total points
ID: 8028363


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
 
LVL 6

Expert Comment

by:carchitect
ID: 8031652
$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
 
LVL 2

Expert Comment

by:laurly
ID: 8031823
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question