473,412 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

question about processing MySQL query result

after executing the query, say it returns 1000 results.

if i just want to do some sampling and like to have a look on 4 of them,
how can i refer to these 4 random records out of 1000? i'm working on
processing the result of $res_array which has 1000 records.

query = select * from test;

result = mysql_query(query);

records1000 = db_result_to_array($result);
//how to select/remove records1000 to get say, records4?
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count] = $row;
}
return $res_array;
}
Jun 22 '06 #1
4 1788
in the query itself you could add 'order by rand() limit 4'

vito wrote:
after executing the query, say it returns 1000 results.

if i just want to do some sampling and like to have a look on 4 of them,
how can i refer to these 4 random records out of 1000? i'm working on
processing the result of $res_array which has 1000 records.

query = select * from test;

result = mysql_query(query);

records1000 = db_result_to_array($result);
//how to select/remove records1000 to get say, records4?
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count] = $row;
}
return $res_array;
}


Jun 22 '06 #2

"strawberry" <za*******@gmail.com> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
in the query itself you could add 'order by rand() limit 4'


It works!

Now i'd like to implement a page function to separate 1000 records into 20
pages. But since the associative array does not use numeric index, i'm
unable to do so. Is there any good methods?

p.s. for numeric index:

I can generate, e..g.

Page 1 2 3 4 5 6 ...

and then calculate the mathematics and say, display a table by:

record[pageno*20+offset] record[pageno*20+offset+1]
record[pageno*20+offset+2]

and so on.
Jun 22 '06 #3

vito schreef:
Now i'd like to implement a page function to separate 1000 records into 20
pages.


Add a LIMIT clause to the query.
First page: LIMIT 0, 20
Second page: LIMIT 20, 20
and so on.

http://dev.mysql.com/doc/refman/4.1/en/select.html

Jun 22 '06 #4
On Thu, 22 Jun 2006 01:00:32 -0700, Sjoerd wrote:
Now i'd like to implement a page function to separate 1000 records into
20 pages.


Add a LIMIT clause to the query.
First page: LIMIT 0, 20
Second page: LIMIT 20, 20
and so on.

http://dev.mysql.com/doc/refman/4.1/en/select.html


But remember this won't work if you do an ORDER BY RAND() on each query
(as the information on each page won't be consistent, you will get records
you've already seen reappearing on latter pages).

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
0
by: Mark Jay Johansen | last post by:
Does anyone know what the relative performance would be of using updateable result sets versus using individual insert or update statements? (Specifically at the moment I'm dealing with JSP...
0
by: Alvin A. Delagon | last post by:
I have a multithreaded application that spawns threads which query a database server. During stress test I encountered some threads failing due "lost connection errors" and sometimes the actual...
5
by: Steve JORDI | last post by:
Hi, I have a strange behavior when using IExplorer over FireFox. There is an html form that asks for the name of a city and has a dedicated field for that with a submit button next to it. In...
7
by: Comagmbh | last post by:
Hello! I am new to this group and I hope anyone can help me. I have an error message which is very complicated to me. Okay this message is very simpel, but I don`t understand how to build my SQL...
10
by: e_matthes | last post by:
Hello everyone, I have read many threads about concurrency issues, and I think I understand some of the pieces, but not the whole picture. I believe I am like many people using php: ...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
11
by: rich | last post by:
I'm having a tough time figuring out which of these two options are best. This is a matter of processing my data in PHP, vs MySQL. Usually that's a no brainer, but I have a couple gotchyas here...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.