Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:13:36+00:00 2026-06-01T08:13:36+00:00

I have this table, ——————————————– | products_id | related_products_ids | | —————————————– | 1

  • 0

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:13:38+00:00Added an answer on June 1, 2026 at 8:13 am

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

    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>' : '';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table, -------------------------------------------- | products_id | related_products_ids | | ------------------------------------------ | 1
I have this table: and I want to display all friend names are without
I have this table. I want to select back the items that don't exist
I have this table that has a lot of book related fields including keywords.
I have this table I created manually, and I want to use a gridview
i have this table in database which called data and i want make this
I have this table where I can generate dynamic rows (which has input <type=text
I have this table on MySQL. I want to query the port number for
I have this table: old_id integer NOT NULL, new_id integer Now I want to
I have this table view UserName Product NumberPurchaces -------- ------- --------------- 'John Doe' 'Chair'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.