Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,296 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 858 people online right now. Registration is fast and FREE... Join Now!




Strange error message

 
Reply to this topicStart new topic

Strange error message

Aesa
30 Nov, 2008 - 10:40 PM
Post #1

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 23

I am not sure if this is a PHP or MySQL issue. I test my pages on my local machine before uploading to a live server. I get the following error message when using a variable like this:
CODE

foreach ($out[c2r4][schems] as $i => $v) {
echo "<a href=\"showschems.php?id=$v[schem_abrev]\">$v[schem_name]</a> <br>";
}

where I am drawing data from an array created with:
CODE

$skills = $db->pQuery("SELECT * FROM skills WHERE prof_id = '$prof'");

foreach ($skills as $i => $v)
{    
$out[$v[skl_lvl]] = $v;
}


Warning: Invalid argument supplied for foreach() in on line 206.

The thing is, the pages work flawlessly on the live server. I have Apache 2.2 and the latest releases of MySQL and PHP installed. Can anyone offer an explanation and/or solution?

This post has been edited by Aesa: 30 Nov, 2008 - 10:43 PM

User is offlineProfile CardPM
+Quote Post


CTphpnwb
RE: Strange Error Message
30 Nov, 2008 - 10:49 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
It looks to me like $skills is not an array, but I don't know for sure what $db->pQuery does.


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Strange Error Message
30 Nov, 2008 - 10:50 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well remember that foreach iterates through arrays. The first argument is suppose to be an array. In your first example you actually have a two dimensional array which is fine, if that value in the two dimensional array was itself an array. Some how I doubt this is the case.

From your code there showing you filling it, you are actually creating an array and at each subscript placing in an array $v.

So your foreach should be looking like...

CODE

// Loop through $out making $v the array stored in the $out array.
foreach ($out as $i => $v) {
echo "<a href=\"showschems.php?id={$v[schem_abrev]}\">{$v[schem_name]}</a> <br>";
}


Another note here is that arrays used inside double quotes like you have here, to be evaluated should be in curly braces.

See if that is what you were meaning to say and if it will work out for you. Let us know how it turns out.

smile.gif


User is offlineProfile CardPM
+Quote Post

Aesa
RE: Strange Error Message
1 Dec, 2008 - 08:48 AM
Post #4

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 23

QUOTE(Martyr2 @ 30 Nov, 2008 - 10:50 PM) *

Well remember that foreach iterates through arrays. The first argument is suppose to be an array. In your first example you actually have a two dimensional array which is fine, if that value in the two dimensional array was itself an array. Some how I doubt this is the case.
<-- Snip -->



Actually, the value is an array.
CODE

$skills = $db->pQuery("SELECT * FROM skills WHERE prof_id = '$prof'");

// Resort the array
foreach ($skills as $i => $v)
{    
$out[$v[skl_lvl]] = $v;

//Execute second Query
$out[$v[skl_lvl]]['schems'] = $db->pQuery("SELECT * FROM emu_schems WHERE prof_id = '$prof' AND skl_lvl = '$v[skl_lvl]'");


pQuery is a function in my class file that returns the results in an array, runs stripslashes, etc. so i don't have to code all that stuff for every page I create.

To reiterate, I only get this error on my desktop. The code executes perfectly on the live server which is why I think it may be some sort of config error on my desktop.

User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Strange Error Message
1 Dec, 2008 - 10:18 AM
Post #5

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
QUOTE(Aesa @ 1 Dec, 2008 - 08:48 AM) *
The code executes perfectly on the live server which is why I think it may be some sort of config error on my desktop.

Or:
There might be a difference between the code you're running on both machines. Are you using the same files?


User is offlineProfile CardPM
+Quote Post

Aesa
RE: Strange Error Message
1 Dec, 2008 - 01:56 PM
Post #6

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 23

QUOTE(CTphpnwb @ 1 Dec, 2008 - 10:18 AM) *

QUOTE(Aesa @ 1 Dec, 2008 - 08:48 AM) *
The code executes perfectly on the live server which is why I think it may be some sort of config error on my desktop.

Or:
There might be a difference between the code you're running on both machines. Are you using the same files?


Exactly the same files.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Strange Error Message
1 Dec, 2008 - 02:11 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well something is different and it has to do with that array. Maybe your query result is not yielding what you think it is on one of the servers. The error is telling you that the argument supplied to the foreach is not right. So to find out what, put a print_r($skills) or print_r($out[c2r4][schems]) statement just before line 206. My guess is that you are going to find out that $out[c2r4][schems] or $skills (whichever is line 206) is probably going to be null or at least not an array.

Right now who cares if it works on one and not the other, focus on debugging the one that it is not working on, find out if that is the case, then you have a starting point of finding out what it is.

wink2.gif

This post has been edited by Martyr2: 1 Dec, 2008 - 02:12 PM
User is offlineProfile CardPM
+Quote Post

Aesa
RE: Strange Error Message
2 Dec, 2008 - 09:46 AM
Post #8

New D.I.C Head
*

Joined: 15 Nov, 2008
Posts: 23

OK, I can't believe I am going to admit this to the world because it is going to make me look like a freaking idiot, but I discovered the problem ....

I didn't have the table imported to my local db, therefore it was generating an error because it could not find the data.

You may now laugh hysterically, call me names and throw stuff at me.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Strange Error Message
2 Dec, 2008 - 10:13 AM
Post #9

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
All I have to say is that listen to your errors. If it is saying that there is a problem with an array parameter in a foreach, ask yourself why and check out what it is. Because if you had done that you would have quickly seen the error.

But either way it is good that you asked. Others might have the same problem and now you have given them another thing to check. Even silly questions can help others that is why we always encourage you to ask on the board in the public area.

smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:40PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month