Can a pagination do with only one sql query? [duplicate] - php

This question already has answers here:
MySQL pagination without double-querying?
(9 answers)
Closed 7 years ago.
As I know a pagination structure requires minimum two sql query.
First, find total row
Second, limit your query.
Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?

You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.
However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.

Related

Why have 2 values when i use query select in php? [duplicate]

This question already has answers here:
PHP PDO retrieves duplicate data
(2 answers)
Closed 1 year ago.
I'm newbie in PHP and when I use PHP to query select, I got 2 values, I realize it may be make slow if have 2 duplicated values.
So I want to ask about it and I also want to get just 1 values from select
thanks for reading my question.
here is picture:
It dipends on what your i guess sql query respondes to your query. However if you want cut down your result in sql you can use LIMIT 1
SELECT ...
FROM ...
WHERE ...
LIMIT 1

Mysql COUNT(*) and myql_num_rows in php: Which is faster? [duplicate]

This question already has answers here:
SELECT COUNT() vs mysql_num_rows();
(3 answers)
Closed 7 years ago.
I wan to know the difference between getting row count via select COUNT(*) from table fetching data AND mysqli_num_rows($ofquery)(in php)?
I tried in both ways and works well. But method is faster?
Use COUNT, internally the server will process the request differently.
When doing COUNT, the server will only allocate memory to store the result of the count.
When using mysqli_num_rows, the server will process the entire result set, allocate memory for all those results, and put the server in fetching mode, which involves a lot of different details, such as locking.
Think of it like the following pseudo scenario:
1) Hey , how many people are in the class room? (count)
2) Hey , get me a list of all the people in the classroom, ... I'll calculate the number of people myself (mysqli_num_rows)
count is the best than mysqli_num_rows
Reference from here.

PHP Select random but variabeles have different weights [duplicate]

This question already has answers here:
MySQL: Select Random Entry, but Weight Towards Certain Entries
(11 answers)
Closed 8 years ago.
I'm working on a simple advertisement script, I have a database with all the ads and a weight.
The ad is a picture and the weight is a number from 1 to 20 depending on it's importance (1 shows up rarely and 20 shows up a lot.
But if I'm getting this information from my database how do I sort and choose an ad to display? So can I put a weight on a variabele in a creative way? It's possible to make and array for each weight point and then select a random ad from that array but is there a better alternative?
#Dagon is correct. Use the example from that other question. Again Dagon found it here: MySQL: Select Random Entry, but Weight Towards Certain Entries The comments are really helpful. You basically multiple that row by its weight. To add to that question and answers I would add the rows into an array and then randomly pick one (one array value) to display. When something has more weight just place it in that array times its weight.

How to return the latest added rows from MySql database. [duplicate]

This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.

how to extend the maximum number of expression in a list of oracle? [duplicate]

This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.

Categories