PHP / MySQL using constrained ORDER BY

I created pagination something like this:

http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle

pretty cool. Although I have a problem with my request.

It works great:

require 'includes/function.php';

    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    $limit = 8;
    $startpoint = ($page * $limit) - $limit;

    $statement = "cars WHERE deleted = 'no'";

$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ");

while ($row = mysql_fetch_assoc($query)) {

      

However, when I try to add an ORDER BY to this, like this:

require 'includes / function.php';

    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    $limit = 8;
    $startpoint = ($page * $limit) - $limit;

    $statement = "cars WHERE deleted = 'no'";

$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ORDER BY model DESC");

while ($row = mysql_fetch_assoc($query)) {

      

or just change the operator like this:

$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";

      

I am getting this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.

      

Line 79 is the line:

 while ($row = mysql_fetch_assoc($query)) {

      

Can anyone tell me how I am not using ORDER BY correctly, this puzzled me.: /

+3


source to share


3 answers


Try the query below

$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");

      



If you're wrong, LIMIT should come after the ORDER BY. More details

+3


source


Modify your query:



$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit}") ;

      

0


source


$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");

      

0


source







All Articles