Fetching an array from MySQL in one step

admin

Administrator
Staff member
the PHP MySQL API functions work very well
for small tables, but when you have 10s to
100s of thousands of rows in a table, you
burn significant CPU time just mysql_fetch_row'ing through the result set.

the way i used to do it was this:
<pre>
$someArr = array();
while( $row = mysql_fetch_row($res) ) {
$someArr[] = $row[0];
}
</pre>

This would work ok, but I also found this
optimization speeds up things a little bit:

<pre>
$someArr = array();
while( list($val) = mysql_fetch_row($res) ) {
$someArr[] = $val;
}
</pre>

Incidentally, you could also do this:

<pre>
$someArr = array();
while( list( $someArr[] ) = mysql_fetch_row($res) );
array_pop($someArr);
</pre>

You need the array_pop because the last value
returned from mysql_fetch_row is guaranteed
to be false.

My problem is, that even with those optimizations,
when i'm iterating through 10s of thousands of numbers, it significantly slows down the web
server!

Does anyone know of a way to directly access
the mysql result set (the data is already there!), or some function that will just allocate a php array with the values from
the result set?

The struct for the result set can be found here: (<!-- m --><a class="postlink" href="http://lxr.php.net/source/php4/ext/mysql/libmysql/mysql.h#152">http://lxr.php.net/source/php4/ext/mysq ... ysql.h#152</a><!-- m -->)

i've thought about hacking a new mysql API function myself, but wanted to know if anyone else knew how to do this.

Incidentally, is there any way to get at pointers from php anyway?

Please feel free to email me (<!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->).

Thanks in advance!

Kennon Ballou
<!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->
 
Back
Top