Jump to content

Foreach / Fetch Frustration


kingnutter

Recommended Posts

Can anybody tell me why the foreach loop below is only giving me two results when there should be more?

 

<?php 

$query="SELECT genre_id FROM genrelinkcd WHERE moj_id=$id";

$result=mysql_query($query);
$rows=mysql_fetch_array($result);

foreach ($rows as $row)

{

echo "$row". ', ';

}
?>"

 

I'm always getting stuck on mysql_fetch_array, mysql_fetch_rows, etc despite having to rack my brains and Googling the answer for two days each time. Can anyone point me to a tutorial that spells it all in layman terms?

Link to comment
https://forums.phpfreaks.com/topic/172386-foreach-fetch-frustration/
Share on other sites

Because it should be:

 

while ($row = mysql_fetch_assoc($result))

 

mysql_fetch_*() retrieves one record from the result set in differing formats (object(), array(), assoc() (proxy to array($result, MYSQL_ASSOC), row() (proxy to array($result, MYSQL_NUM)

1. Is $id set?

2. Are there more than 2 rows with that id?

3. Good practise to use backticks (`) the button above the tab button around SQL Tables e.g. SELECT genre_id FROM `genrelinked` WHERE etc.... although this wont fix your problem.

 

That's all I can help you with as I don't understand the foreach statement :\ lol.

 

http://www.brainbell.com/tutors/php/php_mysql/Using_foreach_Loops_with_Arrays.html - this looks helpful and I'm starting to understand it already =D

 

oh and I thought of ignace's answer but then I thought the associative bit is useless however he may be correct....

Curse you ignace! curse you!

 

He's right, my post is a waste of time.

 

Lol,

Do I get banned for "self-flaming"?

 

LOL all I said was LOL and you are already cursing? I didn't say your post is a waste of time. And it's quiet hard to get banned here, so..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.