Display sql results in groups - php

I have an sql query here and it returns a number of results. I'd like to show these results in groups.
What I mean is,
show the first 20 results in some part of the page,
show the next 20 results in another part of the page
etc...
How can I do that?
*I'm using PHP to display results.

What you want is called pagination and the specific implementation depends on the database. For example, in MySQL you can use LIMIT a,b, and most other databases you can use either TOP(n) or ROW_NUMBER.

do your SELECT command with LIMIT statement.
on first query you can get first 20 results, then next 20, etc.

Ok I found a solution in a forum. It's here in case somebody else needs it
http://www.phpbuilder.com/board/showthread.php?t=10311631

Related

Solution Mysql Random query migrate when goto another page

I have table Post, user can search Post by find by keyword.
So, i using SQL query by like keyword and order by RAND().
When show result, i using GridView and Pagination data. And has problem, when go to another page. Example from page 1 go to page 2 with same keyword. i Will Query again and order by Rand(). So some data in page 1 can appear in page 2.
That data duplicate and not good.
So how can i solve this problem. and data query when goto page other will same with data in the first query.
I using Yii2 in my project.
Ok, I think I get it. Example MySQL query:
SELECT * FROM articles WHERE name LIKE '%tag%' LIMIT 0, 20;
Or you can use MySQL's MATCH() instead of LIKE, it doesn't matter. Then LIMIT 0, 20 is limit, how many results to show for the first page. For the second page it should be LIMIT 1, 20. This one you know I except. Then you get an array of results in the PHP, and want to get random values. PHP has such a function for you and it is called suffle().
So you just use shuffle($results); and then you can print them with foreach or whatever you use to print the data. Note that shuffle() returns boolean, so do not use $results = shuffle($results);. Hope this helps.

Can MySQL Workbench display the number of selected rows in the query result grid?

I'm currently using MySQL Workbench to write very complex SQL Queries. To compare between different approaches, I need to know how many records have been returned by my query very quickly.
So, is there any way I can see the number of records returned by my query in the result grid as soon as I execute it?
I know that I can go to the 'Form Editor' tab and click on next and it will show me something like (2/179). But that's a very tedious process for me.
The "Action Output" pane (the bottom vertical pane) has a "Response" column that tells you how many rows were returned.
Philip Olson's way is a good one; yet another way is look at Query Stats in the same panel:
Rows sent to client is the one of interest; note Rows examined is not, as it shows how many rows the engine read to generate the results. (In this case, the operation was an inner join.)

Iterating through a sub-section of result resource in PHP-MySQL

In my program I launch an SQL query and get back a result resource. I then iterate through the rows of this result resource using the mysql_fetch_array() function and use the contents of the fields of each row to construct a further SQL query.
The result of launching this second query is the first set of results that I want. However, because the number of results produced by doing this is not many I want to make the search less specific by dropping the last record used to make the query.
e.g. the query which produces the first set of results I want could be:
SELECT uid FROM users WHERE (gender=male AND relationship_status=single
AND shoe_size=10)
I would then want to drop the last record so that my query became:
SELECT uid FROM users WHERE (gender=male AND relationship_status=single)
I have already written code to produce the first query but as I mentioned above I use the mysql_fetch_array function to iterate through ALL of the records. In subsequent "rounds" I only want to iterate through successively less records so that my query is less specific. How can I do this?
This seems like an very inefficient method too - so I'm welcome to any simple ideas which might make it more efficient.
EDIT: Thanks for the reply - Yeah I am actually doing this in my program. I am basically trying to implement a basic search algorithm by taking all the preferences a user has specified in the DB and using it to form a query to look for people with those preferences. So the first time search using all the criteria, then on successive attempts search using one less criteria and negate the user ids which were previously returned. At the moment I am constructing the query from scratch for each "round", but I want to find a way I can do this using the last query
Using the queries above, you could do:
SELECT uid
FROM users
WHERE uid NOT IN (
SELECT uid
FROM users
WHERE
(gender=male
AND relationship_status=single
AND shoe_size=10)
)
This will essentially turn your first query into a sub-query, and use that to negate the results returned. Ie, it will return all the rows, NOT IN the first query.

pagination with php with a possible infinite limit

I know how to create pagination with php if I have 25 results with 5 results each page, but what about if the limit number can change in the hundreds everyday? How can I write something where the php itself will create more pages if the row count itself changes?
usually pagination is made in two steps:
query to count the number of records
unsig the prevoius query, create the query using LIMIT to retrieve the current page
So i don't see the problem o_O, if tomorrow the number of elements change then the first query would return a different result.. and you wont have problems with the pagination
Later, if you want to improve performance you could set a cache system for the count query.
Hope this helps

PHP/MySQL - Show user the number of results their query had, even if you are limiting the number displayed?

Apologies if I am not wording this correctly but what I have at the moment is a search form and the user can type in how many results they want to be returned but what I would like to do is also show the user the total number of matches in the database.
e.g. User selects their search criteria with a limit of 10 but there are 50 matches in the database.
I would like to tell the user is that there are 50 possible results.
One thing I am not sure of is if this can be combined with my existing query or if a separate query will have to be submitted?
The PHP code to generate my queries is quite complicated but if you would like me to post it just let me know.
Thanks.
Have a look at the SQL_CALC_FOUND_ROWS option for mysql.
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows
And yes, you'll need a second query like:
SELECT FOUND_ROWS()
To get the "real" number of 'would have been' results.

Categories