+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Invalid argument supplied for foreach()

  1. #1
    Jesse's Avatar
    Jesse is offline Lord Of The Keys Jesse is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Manila, PH
    Posts
    1,357

    Invalid argument supplied for foreach()

    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) {
    ?>
    x10HOSTING
    Member Since October 2007.

    JESSEMAN.ME
    | iMusicz.net

  2. #2
    lhyman's Avatar
    lhyman is offline x10 Sophmore lhyman is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    198

    Re: Invalid argument supplied for foreach()

    I don't know much but after looking at some foreach examples, the only thing that comes to my mind is, are you missing this " } " ?

    anyways have a look here:

    http://www.tizag.com/phpT/foreach.php
    My Name is Larry Hyman from Montreal. I fix computers on site, see my web site for more details:

    http://hyman.sytes.net

  3. #3
    Jesse's Avatar
    Jesse is offline Lord Of The Keys Jesse is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Manila, PH
    Posts
    1,357

    Re: Invalid argument supplied for foreach()

    Nope. I have the " } " ending tag.
    x10HOSTING
    Member Since October 2007.

    JESSEMAN.ME
    | iMusicz.net

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Invalid argument supplied for foreach()

    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 Code:
    while ($result $results->fetch()) {
        ...

    or you can implement the Iterator interface and create an SQL result iterator.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Invalid argument supplied for foreach()

    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.
    Nothing is always absolutely so.

  6. #6
    iana02 is offline x10Hosting Member iana02 is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    9

    Re: Invalid argument supplied for foreach()

    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:

  7. #7
    Tariqul Islam's Avatar
    Tariqul Islam is offline x10 Sophmore Tariqul Islam is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Bangladesh
    Posts
    182

    Re: Invalid argument supplied for foreach()

    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.



  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Invalid argument supplied for foreach()

    Quote Originally Posted by iana02 View Post
    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.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  9. #9
    iana02 is offline x10Hosting Member iana02 is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    9

    Re: Invalid argument supplied for foreach()

    Quote Originally Posted by xav0989 View Post
    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.

  10. #10
    Jesse's Avatar
    Jesse is offline Lord Of The Keys Jesse is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Manila, PH
    Posts
    1,357

    Re: Invalid argument supplied for foreach()

    If i give someone the FTP Access, can you fix it for me? i'll pay you with x10credits or something.
    x10HOSTING
    Member Since October 2007.

    JESSEMAN.ME
    | iMusicz.net

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. I deleted .htaccess file by mistake!
    By N|me F A T A L 1 T Y in forum Free Hosting
    Replies: 9
    Last Post: 04-06-2009, 09:34 AM
  2. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. GeIP Script Help
    By cybahq in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 07-30-2008, 06:19 AM
  5. GeIP Script Help
    By cybahq in forum Free Hosting
    Replies: 4
    Last Post: 07-30-2008, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers