954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Invalid argument supplied for foreach()

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.

servis
Junior Poster in Training
82 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

servis
Junior Poster in Training
82 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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 <strong>%</strong>links)

There's a percent sign (%) there instead of a dollar sign ($).

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

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:

$result = mysql_query($sql); //Make sure to use a semicolon at the end of a line, not a colon!
$link = array(); //Here is where you declare the array
while ($row = mysql_fetch_array($res))
{
//$row[tableLegends]; <---Not sure what this is for, and since it doesn't appear to have a function I commented it out
$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
}


Also looking at your foreach code, what is %links . Don't you mean $links :

foreach($link as $links)
{
   ...
}
FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

My suggestion stated _before_ the query...

$link = array ();
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
  $link[]=$row; 
}
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

My suggestion stated _before_ the query...

$link = array ();
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
  $link[]=$row; 
}


Exactly..though it does need one minor fix! (But I'm sure you knew this!):

$link = array();
$result = mysql_query($sql); //<-- Semicolon
while ($row = mysql_fetch_array($res))
{
  $link[] = $row; 
}



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
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

Glad you saw that typo too, FlashCreations. I was starting to worry that maybe % was some PHP syntax I'd never heard of before =P

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Glad you saw that typo too, FlashCreations. I was starting to worry that maybe % 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.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! :D

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

I think you're confusing me for the OP ^^;
That would servis, not myself. He hasn't responded yet =)

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

thank you very much for your responses. when i declare arry() before the loop according to your advise, script run fine.

servis
Junior Poster in Training
82 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You