mysql_unbuffered_query()
by Rekha[ Edit ] 2009-12-04 14:11:24
So far we've been using the mysql_query() function to do all our data searching in PHP, and it works well enough for the vast majority of cases. However, consider this: how does the mysql_num_rows() function know how many rows mysql_query() returned? The answer is simple: mysql_query() runs the query, and fetches and buffers it all so that it has the complete result set available. The mysql_num_rows() function then has access to all the rows, and so can return the true row count.
But, what do you do if you have a large number of rows and don't want to wait before MySQL has finished fetching them all before you start using them? In this scenario, mysql_unbuffered_query() comes into play: it executes the query and returns a resource pointing to the result of the query while MySQL is still working, which means you can start reading before the query has finished.
Example:
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Large query
$sql = "SELECT * FROM Person";
mysql_unbuffered_query($sql,$con);
// start working with data
mysql_close($con);
?>
Note:You cannot use mysql_num_rows() and mysql_data_seek() on a result set returned from mysql_unbuffered_query().