Invalid argument supplied for foreach()

Thread Solved

Join Date: May 2008
Posts: 74
Reputation: servis is an unknown quantity at this point 
Solved Threads: 0
servis servis is offline Offline
Junior Poster in Training

Invalid argument supplied for foreach()

 
0
  #1
Sep 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 827
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: Invalid argument supplied for foreach()

 
0
  #2
Sep 3rd, 2009
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 74
Reputation: servis is an unknown quantity at this point 
Solved Threads: 0
servis servis is offline Offline
Junior Poster in Training

Re: Invalid argument supplied for foreach()

 
0
  #3
Sep 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster

Re: Invalid argument supplied for foreach()

 
0
  #4
Sep 3rd, 2009
Originally Posted by servis View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 173
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 13
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

Re: Invalid argument supplied for foreach()

 
0
  #5
Sep 3rd, 2009
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:
  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 :
  1. foreach($link as $links)
  2. {
  3. ...
  4. }
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 827
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: Invalid argument supplied for foreach()

 
0
  #6
Sep 3rd, 2009
My suggestion stated _before_ the query...

  1. $link = array ();
  2. $result = mysql_query($sql):
  3. while ($row = mysql_fetch_array($res))
  4. {
  5. $link[]=$row;
  6. }
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 173
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 13
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

Re: Invalid argument supplied for foreach()

 
1
  #7
Sep 3rd, 2009
Originally Posted by pritaeas View Post
My suggestion stated _before_ the query...

  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!):
  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!).
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster

Re: Invalid argument supplied for foreach()

 
0
  #8
Sep 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 827
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: Invalid argument supplied for foreach()

 
0
  #9
Sep 3rd, 2009
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 173
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 13
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

Re: Invalid argument supplied for foreach()

 
0
  #10
Sep 3rd, 2009
Originally Posted by EvolutionFallen View Post
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.

Originally Posted by pritaeas View Post
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!
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC