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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.