note 32261 modified in function.mysql-unbuffered-query by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Aug 2011 19:19:49 +0000 |
| Subject: | note 32261 modified in function.mysql-unbuffered-query by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182194@lists.php.net to get a copy of this message | ||
Regarding bailing on a really large result, while doing an unbuffered query, there _is_ a way to do
this: kill the thread and exit your processing loop. This, of course, requires having a separate
database link. Something like below does the trick:
<?php
// a db link for queries
$lh = mysql_connect( 'server', 'uname', 'pword' );
// and a controller link
$clh = mysql_connect( 'server', 'uname', 'pword', true );
if ( mysql_select_db ( 'big_database', $lh ) )
{
$began = time();
$tout = 60 * 5; // five minute limit
$qry = "SELECT * FROM my_bigass_table";
$rh = mysql_unbuffered_query( $qry, $lh );
$thread = mysql_thread_id ( $lh );
while ( $res = mysql_fetch_row( $rh ) )
{
/* do what you need to do
* ...
* ...
*/
if ( ( time() - $began ) > $tout )
{
// this is taking too long
mysql_query( "KILL $thread", $clh );
break;
}
}
}
?>
--was--
Regarding bailing on a really large result, while doing an unbuffered query, there _is_ a way to do
this: kill the thread and exit your processing loop. This, of course, requires having a separate
database link. Something like below does the trick:
// a db link for queries
$lh = mysql_connect( 'server', 'uname', 'pword' );
// and a controller link
$clh = mysql_connect( 'server', 'uname', 'pword', true );
if ( mysql_select_db ( 'big_database', $lh ) )
{
$began = time();
$tout = 60 * 5; // five minute limit
$qry = "SELECT * FROM my_bigass_table";
$rh = mysql_unbuffered_query( $qry, $lh );
$thread = mysql_thread_id ( $lh );
while ( $res = mysql_fetch_row( $rh ) )
{
/* do what you need to do
* ...
* ...
*/
if ( ( time() - $began ) > $tout )
{
// this is taking too long
mysql_query( "KILL $thread", $clh );
break;
}
}
}
http://php.net/manual/en/function.mysql-unbuffered-query.php