简体   繁体   English

不显示来自mysql的数据

[英]Not displaying data from mysql

I have this table, 我有这张桌子

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

I want to display those related products in the product page. 我想在产品页面中显示那些相关产品。 How can I do that? 我怎样才能做到这一点?

Example, I'm in the product page where products_id is 1, i want to display the products from a table like table_name1 by related_products_ids . 例如,我在产品页面, products_id是1,我想从象表显示产品table_name1通过related_products_ids

I have used this code for displaying data, 我已使用此代码显示数据,

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

However, it displays nothing.. Is there something wrong with my code? 但是,它什么也没显示。我的代码有问题吗? Do you have any solution for this? 您对此有什么解决方案吗?

Thank you. 谢谢。

Please try with this 请尝试这个

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

In your code 在你的代码中

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

gives you an array of related product ids which is fine. 为您提供了一系列相关的产品ID,这很好。 But then in your loop: 但是然后在您的循环中:

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

you are overwriting the value of $res in each loop iteration so when you do the next query 您将在每次循环迭代中覆盖$res的值,因此当您执行下一个查询时

$result1 = mysql_query($res);

it will only fetch the last related product id from the database. 它只会从数据库中获取最后一个相关的产品ID。

You have placed the actual database query section outside of the loop. 您已将实际的数据库查询部分放置在循环之外。

You should include this within the foreach loop, so that every time, a new query runs and you will receive the results as you anticipated. 您应该将此内容包含在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'];
    }

}

And one more thing, since you are using double quotes to wrap your query, you can shorten it two 还有一件事,由于您使用双引号将查询换行,因此可以将其缩短两个

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

Update 更新资料

Use the following code to ensure the <li> is not empty 使用以下代码来确保<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>' : '';
}

please change only 请仅更改

$row1=mysql_fetch_assoc($result1);

i means 我的意思是

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM