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} ");
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";
So this piece of code doesn't work and I can't figure it out.
$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';
$query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";
Perhaps try:
$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';
if ($productid != ''){
$query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";
} else {
$query = "SELECT * FROM products ORDER BY rand() limit 3";
}
This should return all products if the productId isn't coming over.
Is there any way to count and split results without doing 2 query,
im using a query something like this:
$result = mysqli_query($con,"SELECT * from articles WHERE category = '$category'");
$row = mysqli_fetch_row($result);
$rows = $row[0];
$page_rows = 20;
$last = ceil($rows/$page_rows);
$pagenum = 1;
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result2 = mysqli_query($con,"SELECT * FROM articles order by id desc $limit");
while($row = mysqli_fetch_array($result2)) {
$id = $row['id'];
}
this is working but i dont like that it has 2 queries, any better idea? thank you
$pagenum = 1;
$rows_on_page = 20;
$start = (($pagenum - 1) * $rows_on_page);
$end = ($pagenum * $rows_on_page);
$result = mysqli_query($con, "SELECT * from articles WHERE category = '$category' ORDER BY id DESC LIMIT $start, $end");
while ($row = mysqli_fetch_array($con,$result) {
... do stuff with articles ...
$pagenum++;
}
The while loop will protect you from going past the end of the records.
try this query,it will return count of records and pagination (title in query is a field name, change it based on your table):
SELECT aa.countt, title FROM articles , (SELECT COUNT(*) AS countt FROM articles WHERE category = '$category' ) AS aa ORDER BY id LIMIT 5,10
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.
I need to combine the two queries if possible or make them process one after the other. I'm assuming the $Record_Count = $Record_Count + 1; doesn't need to be there twice since that's just for the pagination script. (thanks in advance)
$results = mysql_query("SELECT * load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
while ($data = mysql_fetch_array($results)) {
$Record_Count = $Record_Count + 1;
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
while ($recdata = mysql_fetch_array($rec_res)) {
$Record_Count = $Record_Count + 1;
IF $Record_Count is just counting the number of returned rows you could always use mysql_num_rows()
$results = mysql_query("SELECT * FROM load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
$Record_Count += mysql_num_rows($result) + mysql_num_rows($rec_res);