I have tried offset in simpledb but it's not working as it was working in MySQL. I want to do paging for my database API in PHP so that I send the pagenumber and pagelength to the query and it will return the data of that page only.
How can I do this in simpledb?
select * from second
where time_stamp is not null and gibid = '54' and gibview = 'O'
order by time_stamp asc limit $pagelength
Offset is not working so I can't add offset in query. I have Googled and find there is next token is returned but I am not getting nexttoken. How to check for nexttoken?
Per the Simpledb team (this question was asked in their forums) using the next token is the way to do it.
If you want to grab, say the 2500-2600th item from a list and not iterate over the first 2500, the simpledb team recommends doing a count(*) up to 2500, cause it is fast, then grabbing the next token from that result and then issuing your real query to pull the names and attributes.
Clever hack to the fact that limit doesn't take a starting index I thought, but should save you some performance. Just wanted to share that.
Done By Using NextToken in simple db
$files = $this->db->select($domain, $query, $offset)
Here $offset is nexttoken string which will pass in query. and will return next page.
$pagelenght should be :
$pagenum = 4; //current page
$numitems = 20; //items per page
$row_from = $pagenum * $numitems - $numitems;
$pagelenght = $row_from.','.$numitems;
in the end pagelenght should look like this
$pagelenght = '0,20'; //first page
$pagelenght = '20,20'; //second page
$pagelenght = '40,20'; //third page
$pagelenght = 60,20'; //forth page
something like this, first number is from which row, and second is number how many items in one page.
I confirm that LIMIT in SimpleDB only takes one argument.
To be sure, I tried the following query (the domain Person exists):
"SELECT * FROM Person LIMIT 20, 20" and I had the following response:
Client error : The specified query expression syntax is not valid.
Even if limit is not specified, SimpleDB applies a default limit 100, the maximum limit is 2500 rows. I want to stress out that LIMIT works differently from other databases. LIMIT 100 means that you'll get 100 results per time, and you'll get another batch of 100 results with the next token (provided that there is enough data of course).
The way to do the pagination is use tokens. Already obtained token can be stored in the session, so it will be possible to go back to previous pages.
Amazon SimpleDB doesn't support OFFSET clauses in its query syntax like some other storage systems do. For example, if you're using pages of 10 items each in your app, here is how you might get the 4th page in MySQL:
SELECT * FROM items LIMIT 30, 10
The LIMIT 30, 10 clause means skip the first 30 records and return the 10 after them. SimpleDB doesn't have this out of the box, but you can simulate it by doing 2 queries. The first is this one, which counts up the first 30 items:
SELECT COUNT(*) FROM items LIMIT 30
Your SimpleDB client library will execute this, returning the count and also a NextToken element in the response. Now you can make this second query to get the 4th page of items:
SELECT * FROM items LIMIT 10
Make sure that you pass along the NextToken into your client library so that the query is resumed from the associated cursor. Any query issued with that next token only operates on the records that weren't counted by the first query, giving you an implicit offset.
From article https://coderwall.com/p/yr-mdg/numbered-paging-in-simpledb-queries
Related
How to limit mysql rows to select newest 50 rows and have a next button such that next 50 rows are selected without knowing the exact number of rows?
I mean there may be an increment in number of rows in table. Well I will explain it clearly: I was developing a web app as my project on document management system using php mysql html. Everything is done set but while retrieving the documents I mean there may be thousands of documents.
All the documents whatever in my info table are retrieving at a time in home page which was not looking good. So I would like to add pages on such that only newest 50 documents are placed in first page next 50 are in second and so on.
But how come I know the exact number of rows every time and I cannot change the code every time a new document added so... numrows may not be useful I think...
Help me out please...
What you are looking for is called pagination, and the easiest way to implement a simple pagination is using LIMIT x , y in your SQL queries.
You don't really need the total ammount of rows you have, you just need two numbers:
The ammount of elemments you have already queried, so you know where you have to continue the next query.
The ammount of elements you want to list each query (for example 50, as you suggested).
Let's say you want to query the first 50 elements, you should insert at the end of your query LIMIT 0,50, after that you'll need to store somewhere the fact that you have already queried 50 elements, so the next time you change the limit to LIMIT 50,50 (starting from element number 50 and query the 50 following elements).
The order depends on the fields you are making when the entries are inserted. Normally you can update your table and add the field created TIMESTAMP DEFAULT CURRENT_TIMESTAMP and then just use ORDER BY created, because from now on your entries will store the exact time they were created in order to look for the most recent ones (If you have an AUTO_INCREMENT id you can look for the greater values aswell).
This could be an example of this system using php and MySQL:
$page = 1;
if(!empty($_GET['page'])) {
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
if(false === $page) {
$page = 1;
}
}
// set the number of items to display per page
$items_per_page = 50;
// build query
$offset = ($page - 1) * $items_per_page;
$sql = "SELECT * FROM your_table LIMIT " . $offset . "," . $items_per_page;
I found this post really useful when I first try to make this pagination system, so I recommend you to check it out (is the source of the example aswell).
Hope this helped you and sorry I coudn't provide you a better example since I don't have your code.
Search for pagination using php & mysql. That may become handy with your problem.
To limit a mysql query to fetch 50 rows use LIMIT keyword. You may need to find & store the last row id(50th row) so that you can continue with 51th to 100th rows in the next page.
Post what you have done with your code. Please refer to whathaveyoutried[dot]com
check this example from another post https://stackoverflow.com/a/2616715/6257039, you could make and orber by id, or creation_date desc in your query
During fetching data from database with $result = mysqli_query($con,"select * from MY_table, it display 100 columns at a time. my main aim is to display 10 columns at a time, with the help of next and previous buttons user gonna read remaining. is it possible to do with Javascript , or AJAX, or PHP alone.
The word you're looking for is 'pagination'. You can divide your query results into pages. PHP can fetch a part of the query results, and you can click on a button or link to fetch the next page. That next page can be loaded by an entire page refresh, or by Ajax. In either case, PHP fetches the next results and returns them.
9lessons.info has a tutorial on this exact same topic. Well, more a bunch of snippets than a tutorial, but still..
If you're just starting: forget about Ajax at first. Start with reading about the LIMIT clause in MySQL. It allows you to query a subresult of the query, by limiting the returned rows to a specific range.
You can pass a page (or a range) in the url, so when you click 'Next page', you can just call PHP again with ?page=2 or something in the url. Based on that number, you can query a different range of rows.
The last step is to update only a part of the page using Ajax instead of doing a full page refresh.
You can use limit to your sql query to do this
$start = 0;
$result = mysqli_query($con,"select * from MY_table limit ".$start.", 10");
initially $start will be 0 the change the $start variable as per your prev next ..
I am making a simple message board in PHP with a MySQL database. I have limited messages to 20 a page with the 'LIMIT' operation.
An example of my URL is: http://www.example.com/?page=1
If page is not specified, it defaults to 1. As I mentioned earlier, there is a limit of 20 per page, however if that is out of a possible 30 and I wish to view page 2, I only end up with 10 results. In this case, the LIMIT part of my query resembles LIMIT 20,40 - How can I ensure in this case that 20 are returned?
I would prefer to try and keep this as much on the MySQL side as possible.
EDIT:
To clarify, if I am on page 2, I will be fetching rows 20-30, however this is only 10 rows, so I wish to select 10-30 instead.
EDIT:
I am currently using the following query:
My query:
SELECT MOD(COUNT(`ID`),20) AS lmt WHERE `threadID`=2;
SELECT * FROM `msg_messages` WHERE `threadID`=2 LIMIT 20-(20-lmt) , 40-(20-lmt) ;
There are 30 records this matches.
I'm not sure to really understand the question, but if I do I think that the best practive would be to prevent users to go to a page with no results. To do so, you can easily check how many rows you have in total even if you are using the "LIMIT" clause using SQL_CALC_FOUND_ROWS.
For example you could do:
Select SQL_CALC_FOUND_ROWS * from blog_posts where balblabla...
Then you have to run another query like this:
Select FOUND_ROWS() as posts_count
It will return the total rows very quickly. With this result and knowing the current page, you can decide if you display next/prev links to the user.
You could do a:
SELECT COUNT(*)/20 AS pages FROM tbl;
..to get the max number of pages, then work out if you're going to be left with a partial set and adjust your paging query accordingly.
This is a problem with a ordering search results on my website,
When a search is made, random results appear on the content page, this page includes pagination too. I user following as my SQL query.
SELECT * FROM table ORDER BY RAND() LIMIT 0,10;
so my questions are
I need to make sure that everytime user visits the next page, results they already seen not to appear again (exclude them in the next query, in a memory efficient way but still order by rand() )
everytime the visitor goes to the 1st page there is a different sets of results, Is it possible to use pagination with this, or will the ordering always be random.
I can use seed in the MYSQL, however i am not sure how to use that practically ..
Use RAND(SEED). Quoting docs: "If a constant integer argument N is specified, it is used as the seed value." (http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand).
In the example above the result order is rand, but it is always the same. You can just change the seed to get a new order.
SELECT * FROM your_table ORDER BY RAND(351);
You can change the seed every time the user hits the first results page and store it in the user session.
Random ordering in MySQL is as sticky a problem as they come. In the past, I've usually chosen to go around the problem whenever possible. Typically, a user won't ever come back to a set of pages like this more than once or twice. So this gives you the opportunity to avoid all of the various disgusting implementations of random order in favor of a couple simple, but not quite 100% random solutions.
Solution 1
Pick from a number of existing columns that already indexed for being sorted on. This can include created on, modified timestamps, or any other column you may sort by. When a user first comes to the site, have these handy in an array, pick one at random, and then randomly pick ASC or DESC.
In your case, every time a user comes back to page 1, pick something new, store it in session. Every subsequent page, you can use that sort to generate a consistent set of paging.
Solution 2
You could have an additional column that stores a random number for sorting. It should be indexed, obviously. Periodically, run the following query;
UPDATE table SET rand_col = RAND();
This may not work for your specs, as you seem to require every user to see something different every time they hit page 1.
First you should stop using the ORDER BY RAND syntax. This will bad for performance in large set of rows.
You need to manually determine the LIMIT constraints. If you still want to use the random results and you don't want users to see the same results on next page the only way is to save all the result for this search session in database and manipulate this information when user navigate to next page.
The next thing in web design you should understand - using any random data blocks on your site is very, very, very bad for users visual perception.
You have several problems to deal with! I recommend that you go step by step.
First issue: results they already seen not to appear again
Every item returned, store it in one array. (assuming the index id on the example)
When the user goes to the next page, pass to the query the NOT IN:
MySQL Query
SELECT * FROM table WHERE id NOT IN (1, 14, 25, 645) ORDER BY RAND() LIMIT 0,10;
What this does is to match all id that are not 1, 14, 25 or 645.
As far as the performance issue goes: in a memory efficient way
SELECT RAND( )
FROM table
WHERE id NOT
IN ( 1, 14, 25, 645 )
LIMIT 0 , 10
Showing rows 0 - 9 (10 total, Query took 0.0004 sec)
AND
SELECT *
FROM table
WHERE id NOT
IN ( 1, 14, 25, 645 )
ORDER BY RAND( )
LIMIT 0 , 10
Showing rows 0 - 9 (10 total, Query took 0.0609 sec)
So, don't use ORDER BY RAND(), preferably use SELECT RAND().
I would have your PHP generate your random record numbers or rows to retrieve, pass those to your query, and save a cookie on the user's client indicating what records they've already seen.
There's no reason for that user specific data to live on the server (unless you're tracking it, but it's random anyway so who cares).
The combination of
random ordering
pagination
HTTP (stateless)
is as ugly as it comes: 1. and 2. together need some sort of "persistent randomness", while 3. makes this harder to achieve. On top of this 1. is not a job a RDBMS is optimized to do.
My suggestion depends on how big your dataset is:
Few rows (ca. <1K):
select all PK values in first query (first page)
shuffle these in PHP
store shuffled list in session
for each page call select the data according to the stored PKs
Many rows (10K+):
This assumes, you have an AUTO_INCREMENT unique key called ID with a manageable number of holes. Use a amintenace script if needed (high delete ratio)
Use a shuffling function that is parameterized with e.g. the session ID to create a function rand_id(continuous_id)
If you need e.g. the records 100,000 to 100,009 calculate $a=array(rand_id(100,000), rand_id(100,001), ... rand_id(100,009));
$a=implode(',',$a);
$sql="SELECT foo FROM bar WHERE ID IN($a) ORDER BY FIELD(ID,$a)";
To take care of the holes in your ID select a few records too many (and throw away the exess), looping on too few records selected.
I'm wondering what is command for database query that is querying data forward or back between rows? Just like forward and back button. Can someone give me an example? Thanks.
For examp:
$sql_xxxx = mysql_query("SELECT * FROM xxxx WHERE id='$xxx'") or die (mysql_error());
$row_xxxx = mysql_fetch_array($sql_xxxx);
Now I need the two same query but for one forward and in the other backward the data.
Since you tagged PHP and MySQL I think I can guess what you are trying to do;
Usually you can control this with the ID of a record in MySQL, for example:
SELECT * FROM MYTABLE WHERE ID = 1;
In PHP, you can construct dynamic queries to get different results while just changing the value of a variable.
Lets say we now want to take the same query and make it dynamic:
$id = 1;
$query = "SELECT * FROM MYTABLE WHERE ID = $id ";
as long as you execute this query, it will give you the row with ID = 1 , cause now it is taking the value from the $id variable.
If you want the next row, then you execute the same query but now $id must have the next value.
$id = 2;
Having a "NEXT" and "BACK" button can be matter of just adding or substracting 1 to the $id variable.
Still, this is only an example. In most of the cases you should not play with id's like that cause you should not assume that all id's exist, remember that "DELETE" exist;
So you can try to execute better a little query to find the next value like this:
to go foward:
$query= "SELECT MIN(ID) FROM MYTABLE WHERE ID > $id"
// This will get you for sure the closest id next to the current one
and to go back:
$query= "SELECT MAX(ID) FROM MYTABLE WHERE ID < $id"
I hope this helps,
Regards
You can specify a LIMIT in your query, which results in a specific number of rows. For instance, when you end your query with LIMIT 100, 10, you will get 10 results starting at row 100.
This way, you can build prev/next buttons, that when clicked, retrieve the previous or next page of results.
But in most other cases, you will just query everything you need and run through the result set. You do this by querying the result, and fetching each next row of the result.
If you use the LIMIT keyword, the database will happily return a "page" of data, for example;
SELECT * FROM posts ORDER BY post_time LIMIT 40,20; -- will return rows 41-60.
You can make use of that from PHP by just setting the first LIMIT number to page number * page size and the second number to page size (with page number being 0 for the first page). In the example, the page size would be 20 and the page number would be 2 (the third page) and the results will be all the posts from the third page.
There's nothing more to it from the database side, you'll just have to make good use of it from PHP.
if i'm understanding the question correctly, it sounds like you're trying to implement pagination in your queries and you would utilize the 'limit' clause, like so:
SELECT * FROM `your_table` LIMIT 5, 5
This will show records 6, 7, 8, 9, and 10
here's more documentation