0

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.

4
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by servis
Featured Replies
  • [QUOTE=pritaeas;965973]My suggestion stated _before_ the query... [code=php] $link = array (); $result = mysql_query($sql): while ($row = mysql_fetch_array($res)) { $link[]=$row; } [/code][/QUOTE] Exactly..though it does need one minor fix! (But I'm sure you knew this!): [code=PHP] $link = array(); $result = mysql_query($sql); //<-- Semicolon while ($row = mysql_fetch_array($res)) { $link[] … Read More

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.

0

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.

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 [B][U]%[/U][/B]links) There's a percent sign (%) there instead of a dollar sign ($).

0

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)
{
   ...
}
0

My suggestion stated _before_ the query...

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

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!).

0

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

0

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.

0

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.

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.