lucas
lucas

Reputation: 1

MYSQL can I change how to LIMIT results dynamicly?

Instead of hardcoding query: LIMIT 3, i would like to get LIMIT number different every time i press the button.

That's what I've got so far:

$region=$_GET["region"];

$number=$_GET["limit"];

$con = mysql_connect('localhost') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());

$sql1="SELECT size,price,member FROM buy  WHERE region= '".$region."' LIMIT = '".$number."'  ORDER BY price DESC " 

Error I get is:

Warning: mysql_fetch_array() expects parameter 1 to be resource

So LIMIT is empty

So I got two buttons: one set $number=3 another set $number=10 but I don't know how to put it inside SQL.

So far only option WHERE region= '".$region."' works.

Any ideas ?

Upvotes: 0

Views: 243

Answers (2)

pkt1975
pkt1975

Reputation: 98

A few points here:

  • The MySQL LIMIT statement can take both a LIMIT and an OFFSET, this is the basics required for paging. See MySQL SELECT Syntax
  • Are you sure that the limit variable is being set on the URL? You are using $_GET['limit'], but if it is not on the URL it will not have a value and it will break your query. Try displaying/logging the SQL statement to see what is happenning.
  • Ideally you should also sanitise all the $_GET variables and ensure they are valid

Upvotes: 1

Emmerman
Emmerman

Reputation: 2343

First - remove quotes around limit number.

Second, remove = before number.

Third - move LIMIT after ORDER BY

i think thats all :)

$sql1="SELECT size,price,member FROM buy  WHERE region= '".$region."' ORDER BY price DESC LIMIT ".(int) $number 

Upvotes: 3

Related Questions