不显示来自mysql的数据

时间:2012-03-28 12:56:55

标签: php mysql sql

我有这张桌子,

--------------------------------------------
|   products_id   |   related_products_ids |
| -----------------------------------------
|    1            |  1,2,3,4,6,            |
| -----------------------------------------
|    2            |   1,2,3,               |
| -----------------------------------------
|    3            |   1,2,                 |
-------------------------------------------

我想在产品页面中显示这些相关产品。我怎么能这样做?

示例,我在products_id为1的产品页面中,我希望通过table_name1显示related_products_ids表格中的产品。

我已使用此代码显示数据

    $sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lst_rp = explode(',', $row['related_products_ids']);
echo '<ul>';
foreach($lst_rp as $rp_id) {
  $res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
  $result1 = mysql_query($res);
  $row1 = mysql_fetch_array($result1);
  echo '<li>'.$row1['products_name'].'</li>';
}
echo '</ul>';

然而,它什么都没显示..我的代码有问题吗?你对此有什么解决方案吗?

谢谢。

4 个答案:

答案 0 :(得分:0)

请尝试使用此

$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lst_rp = explode(',', $row['related_products_ids']);
foreach($lst_rp as $rp_id) {
  $res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
  $result1 = mysql_query($res);
  $row1 = mysql_fetch_array($result1);
  echo $row1['products_name'];
}

答案 1 :(得分:0)

在您的代码中

$lst_rp = explode(',', $row['related_products_ids']);

为您提供一系列相关的产品ID,这很好。但是在你的循环中:

foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE       products_id='" . $rp_id . "'";
}

您在每次循环迭代中覆盖$res的值,因此当您执行下一个查询时

$result1 = mysql_query($res);

它只会从数据库中获取最后一个相关的产品ID。

答案 2 :(得分:0)

您已将实际数据库查询部分放在循环之外。

您应该在foreach循环中包含此内容,以便每次都运行新查询,您将收到预期的结果。

foreach($lst_rp as $rp_id) {

    $res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
    $result1 = mysql_query($res);
    while($row1 = mysql_fetch_array($result1)) {
        echo $row1['products_name'];
    }

}

还有一件事,因为你使用双引号来包装你的查询,你可以缩短它两个

$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='$rp_id'";

更新

使用以下代码确保<li>不为空

foreach($lst_rp as $rp_id) {
  $res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
  $result1 = mysql_query($res);
  $row1 = mysql_fetch_array($result1);
  echo !empty($row1['products_name']) ? '<li>'.$row1['products_name'].'</li>' : '';
}

答案 3 :(得分:0)

请仅更改

$row1=mysql_fetch_assoc($result1);

我的意思是

foreach($lst_rp as $rp_id) {
 $res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
  $result1 = mysql_query($res);
  $row1 = mysql_fetch_assoc($result1);
  echo $row1['products_name'];

}