Forum Moderators: coopster

Message Too Old, No Replies

Using mysql_fetch* functions

         

aspr1n

1:12 pm on Dec 14, 2002 (gmt 0)

10+ Year Member



Hi all,

This question pertains to navigating result sets. I would like to avoid using the LIMIT clause, as I would have to re-run the same query to get the number of results without the clause, to allow me to:

Results [ 21 ] of [ 40 ]

Which seems rather wasteful.

Is there a way of returning an entire array of the results set that then I can get an index into. All the

mysql_fetch*
functions seem to return only one row as an array.

Where as I like to be able to reference a particular index without having to call

mysql_fetch*
'x' number of times to get that row.

cheers,

asp

andreasfriedrich

5:20 pm on Dec 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is indeed no PHP equivalent to Perlīs fetchall_arrayref [search.cpan.org].

If you want only one field from a certain row you could use mysql_result [php.net] which lets you specify the row index directly.

Generally it is good practice to let the db engine do as much work as possible and to retrieve as little data from the db as you need. Limit PHP scripting. MySQL is better at manipulating large data sets than PHP.

If you only want to know the number of rows matching a certain criteria use count in your SQL statement.

Whatīs most efficient will depend on what exacly you are trying to do.

Andreas

aspr1n

1:57 am on Dec 15, 2002 (gmt 0)

10+ Year Member



Thanks andreas,
I'd forgotten all about aggregate functions ;)
Cheers,

asp