tags:

views:

38

answers:

1

I've been grappling with this one for a couple of days now, but I've reached the limits of my admittedly limited mysql knowledge. I have two tables, groups and parties, using a lookup table groupparty with two columns groupid and partyid. Given a particular party, I simply want to pull out the groups associated with that party. Here is the bare bones of the script I've been struggling with:

<?php



include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';

 $partyid = '71';
 $sql = "SELECT groupid FROM groupparty WHERE groupparty.partyid='$partyid'";
 $result = mysqli_query($link, $sql);
 while ($row = mysqli_fetch_array($result))
 {
  $groupids[] = array('groupid' => $row['groupid']);
 }

 $sql = "SELECT groupname FROM groups WHERE id='$groupids'";
 $result = mysqli_query($link, $sql);
 while ($row = mysqli_fetch_array($result))
 {
  $groups[] = array('groupname' => $row['groupname']);
 }

 include 'show.html.php';
 exit();

?>

And the html call in show.html.php:

Group(s):
 <?php foreach ($groups as $group): ?>
 <?php htmlout($group['groupname']); ?>
 <?php endforeach; ?>

Which of course gives me the following error:

 Group(s): 
 Warning: Invalid argument supplied for foreach() in show.html.php on line 2

I know the issue is the attempt to SELECT groupname FROM groups WHERE id='$groupids'. But how does one handle this common situation?

Thanks.

A: 

Use a join to do the whole thing in just one query:

SELECT groups.groupname
FROM groupparty
JOIN groups
ON groups.id = groupparty.groupid
WHERE groupparty.partyid='$partyid'

Or if you want to continue with your way of doing it use the SQL expression IN (..., ..., ...) to match any one of a number of IDs. I wouldn't recommend this though.

Mark Byers
Thanks Mark - so now I have $sql = "SELECT groups.groupname FROM groupparty JOIN groups ON groups.groupid = groupparty.groupid WHERE groupparty.partyid='$partyid'"; $result = mysqli_query($link, $sql); while ($row = mysqli_fetch_array($result)) { $groups[] = array('groupname' => $row['groupname']); }and get the error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in index.php on line 12I've seen that one a couple of times over the past two days... :-D
PeterC
Make that `mysqli_query($link, $sql) or die( mysqli_error($link) . ': ' . $sql );` and take a look at the error message.
VolkerK
@girosole: You got an early version of my query where I typed a column name incorrectly. Try refreshing the page: groups.groupid should be groups.id. And you will need to learn how to debug errors yourself if you want to become a web developer. :)
Mark Byers
@VolkerK @Mark Byers - thanks - I had stripped out the error statements to make my post more concise... That'll teach me...So, now I'm error free - but with no data showing... :-( The sql query is now definitely doing the trick. Thanks. I'll keep plugging away at it...
PeterC
@girosole: It might be easier for you to first try the query in the query browser, get it working there and returning the correct row set. Once you have convinced yourself that the query is correct, add the PHP layer on top. This way you don't have to debug errors both places at once.
Mark Byers
Hah! solved. Thanks to everyone again!
PeterC
@Mark - yep, that's what did it! :-D
PeterC