note 96845 added to function.mysql-num-rows

From: Date: Thu, 18 Mar 2010 19:20:07 +0000
Subject: note 96845 added to function.mysql-num-rows
Groups: php.notes 
Request: Send a blank email to php-notes+get-167178@lists.php.net to get a copy of this message
Actually I am a little ashamed to be saying this, but I stand corrected about a rather old note I posted on 17-Jul-2007 06:44. Using SQL_CALC_FOUND_ROWS and FOUND_ROWS( ) will NOT trigger a race condition on MySQL, as that would pretty much defy their entire purpose. The results for their usage is actually unique per connection session as it is impossible for processes to share anything in PHP. As far as PHP is concerned, each request represents a new connection to MySQL as each request is isolated to its own process. To simulate this, create the following script: <?php $Handle = mysql_connect( "localhost" , "root" , "" ); mysql_select_db( "lls" ); if( isset( $_GET[ 'Sleep' ] ) ) { mysql_query( "SELECT SQL_CALC_FOUND_ROWS bid From blocks Limit 1" ); } else { mysql_query( "SELECT SQL_CALC_FOUND_ROWS aid From access Limit 1" ); } if( isset( $_GET[ 'Sleep' ] ) ) { sleep( 10 ); // Simulate another HTTP request coming in. $Result = mysql_query( "SELECT FOUND_ROWS( )" ); print_r( mysql_fetch_array( $Result ) ); } mysql_close( ); ?> Set the connection and query information for something that matches your environment. Run the script once with the Sleep query string and once again without it. Its important to run them both at the same time. Use Apache ab or something similar, or even easier, just open two browser tabs. For example: http://localhost/Script.php?Sleep=10 http://localhost/Script.php If a race condition existed, the results of the first instance of the script would equal the results of the second instance of the script. For example, the second instance of the script will execute the following SQL query: <?php mysql_query( "SELECT SQL_CALC_FOUND_ROWS aid From access Limit 1" ); ?> This happens while the first instance of the script is sleeping. If a race condition existed, when the first instance of the script wakes up, the result of the FOUND_ROWS( ) it executes should be the number of rows in the SQL query the second instance of the script executed. But when you run them, this is not the case. The first instance of the script returns the number of rows of its OWN query, which is: <?php mysql_query( "SELECT SQL_CALC_FOUND_ROWS bid From blocks Limit 1" ); ?> So it turns out NO race condition exists, and every solution presented to combat this "issue" are pretty much not needed. Good Luck, ---- Server IP: 69.147.83.197 Probable Submitter: 62.114.141.150 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-num-rows.php Edit -- https://master.php.net/note/edit/96845 Del: integrated -- https://master.php.net/note/delete/96845/integrated Del: useless -- https://master.php.net/note/delete/96845/useless Del: bad code -- https://master.php.net/note/delete/96845/bad+code Del: spam -- https://master.php.net/note/delete/96845/spam Del: non-english -- https://master.php.net/note/delete/96845/non-english Del: in docs -- https://master.php.net/note/delete/96845/in+docs Del: other reasons-- https://master.php.net/note/delete/96845 Reject -- https://master.php.net/note/reject/96845 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167178) next »