Query using limit and order by

hi,

I want to display my records 20 at a time.
If I use the query without ORDER BY, it works, but if I try ordering them , I have problems. This seems to make sense to me, but I really need to order them. How can I do this ?

TIA pat

$order_by="typ_art, date_art desc";
$display = 20;
$lower_limit = 1;
$higher_limit=$display ;

$query = "SELECT titre, id_art , typ_art, expire, date_art FROM info limit $lower_limit  , $higher_limit ORDER BY $order_by ";	
$result = @mysql_query ($query); // Run the query.

	$bg = '#eeeeee'; // Set the background color.
	$param = $_GET['type_call'];  // get the type of call , S=supprimer, M=modifier	
	while ($row = mysql_fetch_assoc($result)) :
. . . 
		
	endwhile; 
	

Normally the limit is placed right at the end.

I don’t know if that will help, but what will help is knowing that the MySQL Forum is where people know the most about making MySQL behave properly.

There seem to be a few problems.

First, the ‘order by’ value should be explicit – i.e. if you want ‘typ_art’ ascending, write ASC instead of leaving it blank.

I’d recommend formatting the query using indentation (like below) and comments for simple lines.

The ‘while’ syntax is much easier to read using { } instead of ‘endwhile’.


$order_by = "typ_art, date_art desc";
$display = 20;
$lower_limit = 1;
$higher_limit = $display;

$query = "SELECT titre, id_art , typ_art, expire, date_art
            FROM info
            LIMIT $lower_limit, $higher_limit
            ORDER BY $order_by";
$result = @mysql_query($query);

$bg = '#eeeeee';
$param = $_GET['type_call'];
while ($row = mysql_fetch_assoc($result))
{

}

Now, to your main problem. The limit part is wrong. The first parameter is how many you want returned (say 20), and the second is how many rows to offset by (say you are on page 3, then offset would be 40, because you want to see rows 41-60).

LIMIT 20, 40

AFAIK the LIMIT needs to come after the ORDER BY, not before it.

What has this got to do with the question?

this is almost correct, except you have it completely backwards :slight_smile:

rows 41-60 are returned by LIMIT 40,20

the first parameter is the offset, the second paramter is how many to return

Thanks all, it is working great now .

Pat