从WHILE循环中的另一个表获取变量

时间:2012-02-07 03:06:51

标签: php mysql

<?php
$id = $_SESSION['user_id'] ;
echo "<form method='post' action='#'>";
echo "</select>
<p>Which Hospital Would You Like to Submit To?</p>";
$queryitem  = "SELECT * FROM vendor_hospital WHERE vendor_hospital.user_id = '$id' AND vendor_hospital.approval_status = '1'" or die('MYSQL error: ' . mysql_error());
if ($result = mysql_query($queryitem)) {
    if ($success = mysql_num_rows($result) > 0) {
        echo "<select name='hospital_name'>";
        echo "<option>-- Select A Facility --</option>";
        while ($row = mysql_fetch_array($result))
        echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
        echo "</select><br><br>";
    } else {
        echo "No results found.";
    }
} else {
    echo "Failed to connect to database.";
}
echo "<input type='submit' value='Submit' name='submit' class='button' /></form>";
?>

出于某种原因,我被困在这里。我只是想在我的选项中显示制造商名称而不是manufacturer_id。制造商名称是另一个表中的外键,因此我不能简单地在我的选项标签中调用$row[manufacturer_id]。我该怎么办?我唯一的想法是在选项标签内为每个作为值列出的manufacturer_id运行一个查询,但我确信这是过度的。有人能指出一个比这更优雅的解决方案的正确方向吗?

1 个答案:

答案 0 :(得分:0)

不确定你要做什么......但试试这个。

替换它:

while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";

有了这个:

        while ($row = mysql_fetch_array($result)){
        echo "<option value='".$row['manufacturer_id']."'>".$row['manufacturer_id']."</option>";
}
        echo "</select><br><br>";

另外一定要仔细检查并确保从正确的数据库中取出,填充它,调用正确的表名等等......