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'];
    }
131
  • 1,363
  • 1
  • 12
  • 32
  • Do you have a question? – Sean Bright Jul 10 '13 at 01:16
  • Just FYI, the entire `mysql` set of functions will be deprecated in PHP 5.5. You should teach yourself `mysqli` to stay current. – DevlshOne Jul 10 '13 at 01:25
  • lol, forgot to ask the question xD And thanks for the tip DevlshOne – 131 Jul 10 '13 at 01:25
  • @ahmad albayati: `echo mysql_error();` – zerkms Jul 10 '13 at 01: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 – 131 Jul 10 '13 at 01:31
  • Possible duplicate [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select?answertab=votes#tab-top). – John Conde Jul 10 '13 at 01:57
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Jul 10 '13 at 01:59

2 Answers2

1

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

WHERE `group` = ...
zerkms
  • 249,484
  • 69
  • 436
  • 539
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'
Sean
  • 12,443
  • 3
  • 29
  • 47