note 85654 added to function.mysql-num-rows

From: Date: Wed, 10 Sep 2008 19:18:42 +0000
Subject: note 85654 added to function.mysql-num-rows
Groups: php.notes 
Request: Send a blank email to php-notes+get-144299@lists.php.net to get a copy of this message
In preventing the race condition for the SQL_CALC_FOUND_ROWS and FOUND_ROWS() operations, it can become complicated and somewhat kludgy to include the FOUND_ROWS() result in the actual result set, especially for complex queries and/or result ordering. The query gets more complex, you may have trouble isolating/excluding the FOUND_ROWS() result, and mysql_num_rows() will return the number of actual results + 1, all of which makes your code messier and harder to read. However, the race condition is real and must be dealt with. A alternative and cleaner method is to explicitly lock the table using a WRITE lock (preventing other processes from reading/writing to the table). The downsides I can see are a performance hit, and your mysql user must have lock permissions. <?php // excuse the use of mysqli instead of mysql $mysqli->query("LOCK TABLE t WRITE"); $results = $mysqli->query("SELECT id FROM t LIMIT 0,10"); $totalNumResults = array_pop($mysqli->query("SELECT FOUND_ROWS()")->fetch_row()); $mysqli->query("UNLOCK TABLES"); ?> Now you may iterate through the results just like any other result set. ---- Server IP: 208.69.120.35 Probable Submitter: 137.78.41.201 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-num-rows.php Edit -- https://master.php.net/note/edit/85654 Del: integrated -- https://master.php.net/note/delete/85654/integrated Del: useless -- https://master.php.net/note/delete/85654/useless Del: bad code -- https://master.php.net/note/delete/85654/bad+code Del: spam -- https://master.php.net/note/delete/85654/spam Del: non-english -- https://master.php.net/note/delete/85654/non-english Del: in docs -- https://master.php.net/note/delete/85654/in+docs Del: other reasons-- https://master.php.net/note/delete/85654 Reject -- https://master.php.net/note/reject/85654 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144299) next »