Re: Re: Problem with mysql_fetch_array after first loop...
| From: | Jochem Maas | Date: | Thu, 03 Mar 2005 13:49:38 +0000 |
| Subject: | Re: Re: Problem with mysql_fetch_array after first loop... | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-38751@lists.php.net to get a copy of this message | ||
Steve McGill wrote:
"Jeffrey Baumgartner" <jeffreyb@ungodly.com> schreef in bericht news:4226F88A.9020504@ungodly.com...are you by any chance doing a query inside this while loop? if you are then make sure you assign the result of the call to mysql_query() to a variable that is NOT called $result.I've made a little programme that deletes expired records from database tables. The troublesome bit looks like this...$query = "SELECT ic FROM ic_ic WHERE date <= CURDATE()"; $result = mysql_query($query) or die("Unable to get oldcampaigns because " . mysql_error());while ($row = mysql_fetch_array($result)){ extract($row); ...delete records from tables where ic = ic
print_r(), var_dump() (and plain echo or print) are your friends: if you preceed the line starting 'while ($row =' with the following: var_dump( $result ); and then also add such a statement at the bottom of your while code ie: var_dump( $result ); while ($row = mysql_fetch_array($result)) { extract($row); // not a good idea IMHO // do your stuff var_dump( $result ); } then you should see it change on the second call to var_dump(), which will hopefully lead you to the code that changes the $result var when you don't want it to change.}It works fine for the fitst value of $ic, but on the second time around I get an error message saying that line [starting with "while..."] is not a valid MySQL resource. This really puzzles me because it works one time around, but not after that.
ditto, dont extract() in this situation - its a waste of a function call and a waste of variable initialization... just use the values in the $row array directly... e.g. echo $row['ic']; // this could have been $ic if you had called extract($row)$ic, incidentally, is a string originally generated from the timestamp (ie. getdate[0]) - could this be causing a problem?Why don't you try it without using extract($row), and use $row[variable1] $row[variable2] instead in your loops. extract() pulls all the variables into the global namespace and can be quite dangerous as it might overwrite other variables. (this might be whats happening with the $result variable for example. also, how does extract() behave when trying to convert the [0] [1] [2] variables? you might be better off using mysql_fetch_assoc()
Best wishes, Steve