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";
Related
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";
i have a simple query who select me 3 news from table, but i wont to change this number from other file whith variable.
So this is query:
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT 3';
I tried differently, but did not work...
Help please
My code(i can't answer on my question so i add it here)
$newsAmount = 3;
function get_news() {
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
$result = mysql_query($query);
$news = array();
while ($row = mysql_fetch_array($result)) {
$news[] = $row;
}
return $news;
if (!$result) {
trigger_error('Invalid query: ' . mysql_error() . " in " . $query);
}
}
This is this code.
Actually you can just do this:
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
But make sure to keep the string in double quotes so the variable can be evaluated, Single quotes will be printed out as it is.
Try to echo $query, you will notice that its being printed.
Try this:
$newsAmount = 3;
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT ' + $newsAmount;
you cannot return an array like you did in your code.
I would suggest to do the query inside your main code instead of writing it in a function.
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.
Thank you, very nice solution, that was answered my question.
How can I use your solution if my $sql is as follows:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id) AND ($tbl_main.member_id = $theselect) ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
i.e. how can I use your solution somewhere in the middle of a code.
You can conditionally concatenate the extra condition:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id)";
if(isset($theselect)) {
$sql .= " AND ($tbl_main.member_id = $theselect)";
}
$sql .= " ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
I have a query
public static function TestQuery(
$start=0,
$limit=0){
$sql = "
SELECT count(*) AS total
FROM db.table1
JOIN db.table2
ON table1.fieldID = {$fieldID}
AND table2.assigned = 'N'";
$qry = new SQLQuery;
$qry->query($sql);
if($row = $qry->fetchRow()){
$total = intval($row->total);
}
return $total;
}
which works fine but when I add the limit as below, then it doesnt work and gives me errors
public static function TestQuery(
$start=0,
$limit=0){
$sql = "
SELECT count(*) AS total
FROM db.table1
JOIN db.table2
ON table1.fieldID = {$fieldID}
AND table2.assigned = 'N'";
//this fails
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
//
$qry = new SQLQuery;
$qry->query($sql);
if($row = $qry->fetchRow()){
$total = intval($row->total);
}
return $total;
}
Any help will be appreciated
Put a space in front of LIMIT:
" LIMIT {$startRecord}, {$recordLimit} "
without the space you sql will result in a syntax error.
Edit: This is answer is not correct! MySQL will not error without a space before LIMIT (however, earlier versions of phpmyadmin will incorrectly parse such sql).
Your variables are called $limit and $start:
if($limit > 0) $sql .= " LIMIT {$start}, {$limit} ";
Try changing
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
To
if($recordlimit > 0) $sql .= " LIMIT {$start}, {$limit} ";
It looks like your SQL is getting squished together and should be getting a bad syntax error, and you had the wrong (seemingly) variable names in there.
Wrong variables
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
Solved
Thanks