Invalid argument supplied for foreach()

Jesse

Active Member
Messages
1,360
Reaction score
0
Points
36
Hey guys i need help. How to fix this?

Warning
: Invalid argument supplied for foreach() in /####.com/dev/result.php on line 53

Here is the code on the line 53:
Code:
<?php
foreach ($results as $result) {
?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
When asking about code, always post a minimal test case, the smallest amount of code that illustrates the behavior you're writing about. In this case, you would need to show how $results is set and include the foreach loop's closing brace. This makes your question easier to understand, both for others and yourself. Sometimes the solution becomes obvious upon creating the minimal test case. Remember, we can only answer questions about the code you post. If it's incomplete, we'll waste time going over errors in the posted code that don't exist in your live code.

The error message means $results isn't traversable. If $results is the result of an SQL query, you can use a while loop:
PHP:
while ($result = $results->fetch()) {
    ...
}
or you can implement the Iterator interface and create an SQL result iterator.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
It means that $results is not like an array (or an object in php5).

Without showing the rest of the code there is no way to know what is going on.

Where did $results come from? If it came from a function call, some functions return NULL on an error do differentiate the outcome from an empty array.
 

iana02

New Member
Messages
9
Reaction score
0
Points
0
Descalzo is right, I got this error when what I supposed to be an array was an empty variable.

I really didn't expect PHP to work this way! :eek5:
 

Tariqul Islam

New Member
Messages
182
Reaction score
0
Points
0
The best way to use foreach is as below:

foreach ($array_variable as $key=>$value)
{

}

Description:
========
You have to use an array in place of $array_variable and $key represents here as the array index and the $value represents here as the $array_variable[$key].

I think you can understand. Please let me know if you have any more trouble to use foreach.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Descalzo is right, I got this error when what I supposed to be an array was an empty variable.

I really didn't expect PHP to work this way! :eek5:

You should always check for the array to be initialized beforehand. Anyways, you can check the PHP doc for foreach.
 

iana02

New Member
Messages
9
Reaction score
0
Points
0
You should always check for the array to be initialized beforehand. Anyways, you can check the PHP doc for foreach.
LOL thanks but I've learned that a few years ago already after falling in the trap myself. ;) Now I always check if sizeof($array) > 0 right before the foreach, it works. Even with Tariqul Isla's suggestion, one still has to verify that the array isn't empty.
 

Jesse

Active Member
Messages
1,360
Reaction score
0
Points
36
If i give someone the FTP Access, can you fix it for me? i'll pay you with x10credits or something.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I would be better to post the code here (without any sensitive information stripped out). However, if you'd prefer to not post the code here, send me a PM with all the FTP info and the full name of the file I'll have to check.
 
Top