note 66814 added to function.mysql-data-seek

From: Date: Tue, 30 May 2006 08:52:11 +0000
Subject: note 66814 added to function.mysql-data-seek
Groups: php.notes 
Request: Send a blank email to php-notes+get-112528@lists.php.net to get a copy of this message
A helpful note about the 'resource' data type. Since the 'resource' variable is pointing to a row in a result set at any given time, you can think of it as being passed to this function by reference every time you pass it or assign it to a variable. <? $sql = "SELECT * from <table>"; $result = mysql_query($sql); $temp_result = $result; while ($row = mysql_fetch_assoc($temp_result)) { // do stuff with $row } while ($row = mysql_fetch_assoc($result)) { // This code will never run because the 'resource' variable is pointing past the end of the result set, // even though it was *not* assigned by reference to $result2. } ?> Therefore, the following snipits are functionally identical: <? // Start snipit 1 $sql = "SELECT * from <table>"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { // do stuff with $row } mysql_data_seek($result, 0); while ($row = mysql_fetch_assoc($result)) { // do other stuff with $row } // Start snipit 2 $sql = "SELECT * from <table>"; $result = mysql_query($sql); $temp_result = $result; while ($row = mysql_fetch_assoc($temp_result)) { // do stuff with $row } mysql_data_seek($result, 0); while ($row = mysql_fetch_assoc($temp_result)) { // do other stuff with $row } ?> ---- Server IP: 216.194.113.175 Probable Submitter: 65.100.194.45 ---- X-Spam-Status: No, hits=4.0 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE,HTML_TAG_BALANCE_TABLE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-data-seek.php Edit -- http://master.php.net/note/edit/66814 Del: integrated -- http://master.php.net/note/delete/66814/integrated Del: useless -- http://master.php.net/note/delete/66814/useless Del: bad code -- http://master.php.net/note/delete/66814/bad+code Del: spam -- http://master.php.net/note/delete/66814/spam Del: non-english -- http://master.php.net/note/delete/66814/non-english Del: in docs -- http://master.php.net/note/delete/66814/in+docs Del: other reasons-- http://master.php.net/note/delete/66814 Reject -- http://master.php.net/note/reject/66814 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#112528) next »