note 76463 added to function.mysql-num-rows

From: Date: Tue, 17 Jul 2007 06:44:22 +0000
Subject: note 76463 added to function.mysql-num-rows
Groups: php.notes 
Request: Send a blank email to php-notes+get-128641@lists.php.net to get a copy of this message
A note on the following usage; that suggest to use several MySQL Functions to get the number of Table Records. You may be familiar with following: <?php $sqlQuery = 'Select SQL_CALC_FOUND_ROWS MyField From MyTable Limit 1;'; $sqlQuery_1 = 'Select FOUND_ROWS( );'; ?> I omitted the actual connection to MySQL and the execution of the query, but you get the idea. I did some tests and on a fairly high traffic web site, one that executes several queries quite often and found that using this combination of MySQL Functions can actually result in wrong results. For example, assume I have two queries to get the number of Table Records in two different Tables. So in essence, we are executing 4 queries ( 2 queries for each Table ). If two different requests come in through PHP, your going to run into problems. Note than when I mean request, I mean two different clients requesting your PHP page. --------------- Request 1: --------------- Execute: SQL_CALC_FOUND_ROWS On Table 1 --------------- Request 2: --------------- Execute: SQL_CALC_FOUND_ROWS On Table 2 --------------- Request 1: --------------- Execute: Select FOUND_ROWS( ) At this point, you see the race condition that occurred. While Request 1 was being executed, Request 2 came in. At this point Request 1 will return the number of Table Records in Table 2 and not Table 1 as expected! Why? Because MySQL does not differ between requests. Each query is in a queue waiting its turn. As soon as its turn comes in it will be executed my MySQL. The MySQL Function Select FOUND_ROWS( ) will return the result of the last SQL_CALC_FOUND_ROWS! Keep in mind. ---- Server IP: 69.147.83.197 Probable Submitter: 217.54.224.163 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-num-rows.php Edit -- https://master.php.net/note/edit/76463 Del: integrated -- https://master.php.net/note/delete/76463/integrated Del: useless -- https://master.php.net/note/delete/76463/useless Del: bad code -- https://master.php.net/note/delete/76463/bad+code Del: spam -- https://master.php.net/note/delete/76463/spam Del: non-english -- https://master.php.net/note/delete/76463/non-english Del: in docs -- https://master.php.net/note/delete/76463/in+docs Del: other reasons-- https://master.php.net/note/delete/76463 Reject -- https://master.php.net/note/reject/76463 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128641) next »