By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

question about processing MySQL query result

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.