I am working on a program which will query the database and receive a list of data.
about 100 to 1000 results.
I know that I can use Session to store the arraylist of results in Java (Tomcat)
So that I can process the data without query the database again.
Like i want to show 10 results per page. I just get back the data from session and print it out.
If under PHP, how would you do?
PHP Query Database
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
marquischan
- Forum Newbie
- Posts: 4
- Joined: Thu Jul 20, 2006 2:52 am
Let me point out more detail.
The way i do before in PHP is LIMIT the results by using SQL Query
such as SELECT * FROM ABC LIMIT 0,50
The 0 can be changed !!
However, is there any different between the example below?
SELECT * FROM ABC
under PHP code, i will do
for ($i = 0; $i < 50; $i++)
{
echo($sql_fetched_row['id']);
}
Is there any different? Of course, i will do paging with the two example above
The way i do before in PHP is LIMIT the results by using SQL Query
such as SELECT * FROM ABC LIMIT 0,50
The 0 can be changed !!
However, is there any different between the example below?
SELECT * FROM ABC
under PHP code, i will do
for ($i = 0; $i < 50; $i++)
{
echo($sql_fetched_row['id']);
}
Is there any different? Of course, i will do paging with the two example above
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
marquischan
- Forum Newbie
- Posts: 4
- Joined: Thu Jul 20, 2006 2:52 am
The thing i want to point out here is
When i put ALL rows into the ARRAY.
If i just use START and LIMIT under SQL_query to contraint the number of returned rows, the array size will be decreased much.
Method 1:
SELECT * FROM ABC;
return 1000000 rows, put all into array. and use LOOP , $START and $END to control the print out
I can use count($row) to retrieve the total number of row.
Method 2:SELECT * FROM ABC LIMIT 0,50
Put all into array and print all out.
I need to use SELECT COUNT(*) FROM ABC again to count the total number of row.
Another PROBLEM.
If I show 50 records per page, I need to QUERY the database AGAIN every time i change page.
Is it time consuming and memory consuming?
Course I am doing repeated job. And is there any way to SAVE the query result so that I am no need to query the database again?
Which method will u use?
When i put ALL rows into the ARRAY.
If i just use START and LIMIT under SQL_query to contraint the number of returned rows, the array size will be decreased much.
Method 1:
SELECT * FROM ABC;
return 1000000 rows, put all into array. and use LOOP , $START and $END to control the print out
I can use count($row) to retrieve the total number of row.
Method 2:SELECT * FROM ABC LIMIT 0,50
Put all into array and print all out.
I need to use SELECT COUNT(*) FROM ABC again to count the total number of row.
Another PROBLEM.
If I show 50 records per page, I need to QUERY the database AGAIN every time i change page.
Is it time consuming and memory consuming?
Course I am doing repeated job. And is there any way to SAVE the query result so that I am no need to query the database again?
Which method will u use?
What if the database changes? Someone adds or deletes a record for example. Then your saved query results would be wrong. Just query the database again, it's a lot easier and it'll be the correct data.marquischan wrote:And is there any way to SAVE the query result so that I am no need to query the database again?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA