943,774 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 882
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 3rd, 2009
0

Invalid argument supplied for foreach()

Expand Post »
hi friends,

my code is as follow,

------sql query here------------
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$row[tableLegends];
$link[]=$row; 
}
when i use foreach loop in template, it give the inavlid argument "Invalid argument supplied for foreach() error"
foreach($link AS %links){
--------code here-------
}

i am not understanding where i comit mistake.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
servis is offline Offline
82 posts
since May 2008
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

If the query has no result, $link will be null and that is the invalid argument. If you initialize

$link = array ();

before the query, this will solve that problem.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

I used following code as per your suggestion,
------sql query here------------
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$row[tableLegends];
$link = array ();
$link =$row; 
}

but the script is giving the same error.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
servis is offline Offline
82 posts
since May 2008
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

Click to Expand / Collapse  Quote originally posted by servis ...
foreach($link AS %links){
--------code here-------
}

i am not understanding where i comit mistake.
If that code was a copy and paste from your actual code (as opposed to if you manually typed it into here), I hate to inform you but it's just a typo. You typed:
foreach($link as %links)
There's a percent sign (%) there instead of a dollar sign ($).
Last edited by EvolutionFallen; Sep 3rd, 2009 at 4:58 am.
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

The proper use of this would be declaring the $link variable as an array before you start looping through the array results of the query. Your code should look like this:
PHP Syntax (Toggle Plain Text)
  1. $result = mysql_query($sql); //Make sure to use a semicolon at the end of a line, not a colon!
  2. $link = array(); //Here is where you declare the array
  3. while ($row = mysql_fetch_array($res))
  4. {
  5. //$row[tableLegends]; <---Not sure what this is for, and since it doesn't appear to have a function I commented it out
  6. $link[] = $row; //You were right by approaching this with $link[] and not $link, if you used $link then link would only contain the value of row and won't be an array of the rows
  7. }

Also looking at your foreach code, what is %links . Don't you mean $links :
PHP Syntax (Toggle Plain Text)
  1. foreach($link as $links)
  2. {
  3. ...
  4. }
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

My suggestion stated _before_ the query...

php Syntax (Toggle Plain Text)
  1. $link = array ();
  2. $result = mysql_query($sql):
  3. while ($row = mysql_fetch_array($res))
  4. {
  5. $link[]=$row;
  6. }
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Sep 3rd, 2009
1

Re: Invalid argument supplied for foreach()

Click to Expand / Collapse  Quote originally posted by pritaeas ...
My suggestion stated _before_ the query...

php Syntax (Toggle Plain Text)
  1. $link = array ();
  2. $result = mysql_query($sql):
  3. while ($row = mysql_fetch_array($res))
  4. {
  5. $link[]=$row;
  6. }
Exactly..though it does need one minor fix! (But I'm sure you knew this!):
PHP Syntax (Toggle Plain Text)
  1. $link = array();
  2. $result = mysql_query($sql); //<-- Semicolon
  3. while ($row = mysql_fetch_array($res))
  4. {
  5. $link[] = $row;
  6. }
Also, I believe the problem was that instead of $links , %links as used, but your solution provides a safety if there are no rows returned (Which is a good thing to have!).
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

Glad you saw that typo too, FlashCreations. I was starting to worry that maybe %<varnamehere> was some PHP syntax I'd never heard of before =P
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

Well observed... missed the colon.

I left the template alone, because I was unsure if it was supposed to be php, or a specific templating syntax.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006
Sep 3rd, 2009
0

Re: Invalid argument supplied for foreach()

Glad you saw that typo too, FlashCreations. I was starting to worry that maybe %<varnamehere> was some PHP syntax I'd never heard of before =P
I've never heard of it before and like pritaeas, thought it was some kind of templating application, but assumed you weren't using one.

Click to Expand / Collapse  Quote originally posted by pritaeas ...
Well observed... missed the colon.

I left the template alone, because I was unsure if it was supposed to be php, or a specific templating syntax.
At first I though the same thing, as that seems very logical!

If all your questions have been answered I ask that you mark the thread as solved to avoid further confusion!
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Binary Tree
Next Thread in PHP Forum Timeline: Form submission





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC