| | |
Invalid argument supplied for foreach()
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2008
Posts: 74
Reputation:
Solved Threads: 0
hi friends,
my code is as follow,
when i use foreach loop in template, it give the inavlid argument "Invalid argument supplied for foreach() error"
i am not understanding where i comit mistake.
my code is as follow,
------sql query here------------
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$row[tableLegends];
$link[]=$row;
}foreach($link AS %links){
--------code here-------
}i am not understanding where i comit mistake.
•
•
Join Date: May 2008
Posts: 74
Reputation:
Solved Threads: 0
I used following code as per your suggestion,
but the script is giving the same error.
------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.
•
•
Join Date: Aug 2009
Posts: 47
Reputation:
Solved Threads: 8
•
•
•
•
foreach($link AS %links){ --------code here------- }
i am not understanding where i comit mistake.
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.
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:
Also looking at your foreach code, what is
PHP Syntax (Toggle Plain Text)
$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 : PHP Syntax (Toggle Plain Text)
foreach($link as $links) { ... }
My suggestion stated _before_ the query...
php Syntax (Toggle Plain Text)
$link = array (); $result = mysql_query($sql): while ($row = mysql_fetch_array($res)) { $link[]=$row; }
"If it is NOT source, it is NOT software."
-- NASA
-- NASA
•
•
•
•
My suggestion stated _before_ the query...
php Syntax (Toggle Plain Text)
$link = array (); $result = mysql_query($sql): while ($row = mysql_fetch_array($res)) { $link[]=$row; }
PHP Syntax (Toggle Plain Text)
$link = array(); $result = mysql_query($sql); //<-- Semicolon while ($row = mysql_fetch_array($res)) { $link[] = $row; }
$links , %links as used, but your solution provides a safety if there are no rows returned (Which is a good thing to have!). •
•
•
•
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
•
•
•
•
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 all your questions have been answered I ask that you mark the thread as solved to avoid further confusion!
![]() |
Similar Threads
- Optimizing a lot of foreach loops. Please help. (PHP)
- PHP doesn't like array defined (PHP)
- using foreach with multi-dimensional arrays (PHP)
- Need help making simple random images (PHP)
- The mysterious errors of PHP (PHP)
- paging & _POST variables (PHP)
- Problem with foreach when using linking (PHP)
Other Threads in the PHP Forum
- Previous Thread: Binary Tree
- Next Thread: Form submission
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





