不显示从MySQL数据

我有这样的表,

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

我想在产品页面显示这些相关产品。 我该怎么办呢?

例如,我在产品页面, products_id是1,我想从像一个表格中显示的产品table_name1related_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>';

但是,它不显示任何..有什么错我的代码? 你有没有这方面的任何解决方案?

谢谢。

--------------解决方案-------------

你已经在循环之外的实际数据库查询部分。

您应该将中包含这个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>' : '';
}

请尝试此

$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'];
}

在您的代码

$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。

请只改变

$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'];

}

分类:php 时间:2015-03-15 人气:0
本文关键词: PHP和MySQL,SQL
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.403 (s). 10 q(s)