我一直在努力解決這個問題幾天了,但我已經達到了我公認的有限的mysql知識的極限。我有兩個表,組和派對,使用帶有兩列groupid和partyid的查找表groupparty。鑑於特定的一方,我只想撤出與該方有關的團體。下面是腳本的裸露的骨頭我一直在掙扎:將數據從多對多關係中拉出來
<?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();
?>
而且在show.html.php的HTML調用:
Group(s):
<?php foreach ($groups as $group): ?>
<?php htmlout($group['groupname']); ?>
<?php endforeach; ?>
這當然給了我以下錯誤:
Group(s):
Warning: Invalid argument supplied for foreach() in show.html.php on line 2
我知道問題是SELECT groupname FROM groups WHERE id='$groupids'
的企圖。但是,如何處理這種常見情況呢?
謝謝。
你應該首先初始化'$ groupids'和'$ groups'數組。 – poke 2010-02-06 15:41:37
謝謝 - 這擺脫了一個錯誤! – PeterC 2010-02-06 16:00:04