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
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
My suggestion stated _before_ the query...
$link = array ();
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$link[]=$row;
}
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
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
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
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