Order by and LIMIT both not working together in MySQL Query - php

i'm trying to Order by Desending and want to limit 30 in Query
PHP CODE
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 30;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$query_pag_data = "SELECT * from titles LIMIT $start, $per_page ORDER BY id DESC";
ERROR : MySql ErrorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC' at line 1
PS: i'm using pagination... so limiting 30 result like this

correct syntax is:
SELECT * from titles
ORDER BY id DESC
LIMIT $start, $per_page
LIMIT at the end of the query.

change positions or LIMIT and ORDER BY, like:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";

You have to put the limit at the end of the query for proper syntax:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";

You need to put ORDER BY statement first before LIMIT
Correct syntax is as follows:
SELECT * FROM *table_name* WHERE *condition* ORBER BY *field_name* LIMIT *limit*;

LIMIT statement should always come at the end of the query.

Related

Placing PHP inside SELECT Statement

I want to use php to determine which table my information is coming from but don't know how to place the PHP correctly into the statement.
Say I have the tables:
page1_articles
page2_articles
page3_articles
And I give the page an ID of $page = "page1" or $page = "page2" or $page = "page3"
How can I use this in my SELECT statement:
$get_articles_sql = "SELECT * FROM $page_articles ORDER BY views DESC LIMIT 5";
This should work for you:
$get_articles_sql = "SELECT * FROM " . $page . "_articles ORDER BY views DESC LIMIT 5";
With this you concat $page which could be page1 or page2 or page3 with the string _articles
Edit after comment:
Escape your variable if you didn't do that already! with this:
mysql_real_escape_string($page);
this will work
$get_articles_sql = "SELECT * FROM ".$page_articles." ORDER BY views DESC LIMIT 5";
this code will work better
$get_articles_sql = "SELECT * FROM ".$page_articles." ORDER BY views DESC LIMIT 5";
this code is be best
$get_articles_sql = "SELECT * FROM {$page_articles} ORDER BY views DESC LIMIT 5";

PHP MySQL giving error when applying limit to query

<?php
//pagination
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page = 1;
}
$start_from = ($page - 1) * 10;
$rowLimit = 10; //result limit
?>
<?
//full query
$query = "SELECT * FROM ".mysqli_real_escape_string($games,$_GET['type'])." ORDER BY `Name` ASC";
//query limit
$query_limit = sprintf('%s LIMIT %d %d', $query, $start_from, $rowLimit);
//run full query
$result = mysqli_query($games, $query) or die(mysqli_error($games));
//full rows
$row_all = mysqli_fetch_assoc($result);
//run limited query
$result_limit = mysqli_query($games, $query_limit) or die(mysqli_error($games));
//limited rows
$row = mysqli_fetch_assoc($result_limit);
//number of full rows
$row_all_Num = mysqli_num_rows($result);
//number of limited rows
$row_num = mysqli_num_rows($result_limit);
?>
when I try to open list.php?type=installed MySQL shows this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10' at line 1
if I remove lines that apply limit query works fine but displays all results at once
where is the problem I cant find it....
Use comma between limits
or use LIMIT x OFFSET y
syntax.
echo and then die, it will print the query in your browser and paste it here for others to see.
$query = "SELECT * FROM ".mysqli_real_escape_string($games,$_GET['type'])." ORDER BY `Name` ASC";
echo $query;
die();

OrderBy query Php doesn't work after adding LIMIT

I guess my problem is just about a syntax error or SORT BY should come before LIMIT, anyway after adding LIMIT to my query the following statement generates an mysql error.
$query_pag_data = "SELECT * FROM Apartment LIMIT $start, $per_page";// without LIMIT the if statement works while with LIMIT it doesn't.
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District" ||) {
$query_pag_data .= "ORDER BY ".$_GET['SortBy']; // It doesn't work if I add LIMIT to my query
}
What is the error and how can I make this working with LIMIT and
ORDER BY wout changing my logic.
Your completed SQL should read like SELECT, FROM, ORDER BY, LIMIT. So your PHP should be written like:
$query_pag_data = "SELECT * FROM Apartment";
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
$query_pag_data .= " ORDER BY ".$_GET['SortBy'];
}
$query_pag_data .= " LIMIT $start, $per_page"
this will work
if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
$query_order_by= " ORDER BY ".$_GET['SortBy'];
}
$query_pag_data = "SELECT * FROM Apartment $query_order_by LIMIT $start, $per_page";

Display only the last 300 MYSQL Results with a PHP pagination system

Hi I have a system where I only want to display the last 300 records from MYSQL, normally i would just write the query like this LIMIT 300
the problem i have is i am using a pagination system which writes the query like this.
$tableName="masterip_details";
$targetpage ="raw_data.php";
$limit = 30;
$query = "SELECT COUNT(*) as num FROM $tableName where type='6' AND country_code='GB'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName where type='6' AND country_code='GB' LIMIT $start, $limit";
$result = mysql_query($query1);
The problem is because it uses the limit to calculate the start and finish page numbers i am not sure if i can limit the number of rows to return whilst using the pagination.
select * from (SELECT * FROM $tableName where type='6' AND country_code='GB' order by AUTO_INCERMENT_ID DESC LIMIT 300) as a order by AUTO_INCERMENT_ID ASC LIMIT $start, $limit

PHP/MySQL using ORDER BY with a LIMIT

I created a pagination by roughly following this link:
http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle
quite cool. Although I have an issue with my query.
It works fine 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} ");
while ($row = mysql_fetch_assoc($query)) {
However when I try to add an ORDER BY to this, like so:
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 statement like this:
$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";
I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.
Line 79 is this line:
while ($row = mysql_fetch_assoc($query)) {
Can anyone tell me how I am not using the ORDER BY correctly, its got me puzzled. :/
Try the query as below
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");
Where you have gone wrong is LIMIT should come after ORDER BY. Read more
Change the query:
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit}") ;
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");

Categories