![]() The call to fetch will return false, since you have already exhausted the result set (see above), so you get an error trying to loop over false. Here all results are being output by the first loop. Once you've completed that loop, you need to reset the cursor on the database side before you can loop over it again. That's because the data is being streamed from the database and iterating over the result with foreach is essentially shorthand for: while ($row = $users->fetch()). The first iteration outputs all results, the second does nothing since you can only iterate over the result once. Here $users is a PDOStatement object over which you can iterate. Warning: Invalid argument supplied for foreach()īut when I delete the first "foreach" from the above codes, the output will become normal: " $result = $users->fetch(PDO::FETCH_ASSOC) Running "foreach" twice is not my purpose, I'm just curious why TWO "foreach" statements only output the result once? ![]() But, as discussed, isset($data) will also return false if $data was set, but was set to null. The author of this code presumably wanted to check if keyShouldBeSet was set in $data. do something here if 'keyShouldBeSet' is not set This behavior is more problematic than it might appear at first and is a common source of problems.Ĭonsider the following: $data = fetchRecordFromStorage($storage, $identifier) Unset($value) // $value no longer references $arrĬommon Mistake #2: Misunderstanding isset() behaviorĭespite its name, isset() not only returns false if an item does not exist, but also returns false for null values. To still get the benefit of using references in foreach loops without running the risk of these kinds of problems, call unset() on the variable, immediately after the foreach loop, to remove the reference e.g.: $arr = array(1, 2, 3, 4)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |