0

Okay, so basically, i want to loop mysql_fetch_array inside a foreach loop, like this:

    foreach($groupname as $group)
    {
        $sql2=mysql_query("SELECT * FROM groups WHERE group='$group'");
        $row2=mysql_fetch_array($sql2);
        ?>
        <img src="images/groups/" width="100px" height="100px" /><br />
        <table>
        <tr><td><b>Group: </b></td><td><?php echo $group; ?></td></tr>
        <tr><td><b>Description: </b></td><td><?php echo $row2['description']; ?></td></tr>
        </table><br /><br /><br />
        <?php
    }
    ?>

So when i do that, i get the following mysql error:

  Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...

Any way around this?

Note: Before i do the foreach loop, i do a while loop where i loop through a mysql table, which actually succeed. this is the code snippet for the while loop:

    $groupname=array();
    $sql=mysql_query("SELECT * FROM joined WHERE email='$email'");
    while($row=mysql_fetch_array($sql))
    {
        $groupname[]=$row['group'];
    }
7
  • Do you have a question? Commented Jul 10, 2013 at 1:16
  • Just FYI, the entire mysql set of functions will be deprecated in PHP 5.5. You should teach yourself mysqli to stay current. Commented Jul 10, 2013 at 1:25
  • lol, forgot to ask the question xD And thanks for the tip DevlshOne Commented Jul 10, 2013 at 1:25
  • @ahmad albayati: echo mysql_error(); Commented Jul 10, 2013 at 1:29
  • @zerkms Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group='test'' at line 1 Commented Jul 10, 2013 at 1:31

2 Answers 2

1

You can always check for errors using mysql_error() ie.

$sql2=mysql_query("SELECT * FROM groups WHERE `group`='$group'") or die(mysql_error());

but it is failing as group is a reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
use backticks around group -

SELECT * FROM groups WHERE `group`='$group'
Sign up to request clarification or add additional context in comments.

Comments

1

group is a mysql keyword so you need to enclose it in backticks:

WHERE `group` = ...

2 Comments

Wow, thanks. That fixed the error. My bad. Should have guessed that :P
@ahmad albayati: don't guess - just use mysql_error. It stated it clearly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.