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 8771911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:34+00:00 2026-06-13T17:54:34+00:00

Ok folks. I have a Table categories with id, categoryname. categories has: id=1 categoryname

  • 0

Ok folks. I have a Table categories with id, categoryname.

categories has: id=1 categoryname = batteries, id=2 categoryname = flashes, id=3 categoryname glasses.

The Second table is users. Users have id, user_eimal, my_choices.
Every user has store into my_choices the category name that wants to appear.

So for example user George with user_email xxxxx@xxx.com has store in my_choices: batteries,glasses

Now i want to bring up records from table products where categoryname is the values that user george has stored into my_choices.

Products has id, p_title, categoryname

for example i want to:

 <?php

    $usenmail = $_SESSION['SESS_EMAYL_NAME'];

    $q1 = "SELECT * FROM categories WHERE id = '$id'";
    $q2 = mysql_query($q1, $connectiondb) or die(mysql_error());
    $results = mysql_fetch_assoc($q2);

    $my_choices = $results['my_choices']; //That means $my_choices = batteries,glasses

    //Now i want to select fom my table poducts only the categories (batteries,glasses)that user has store.

$trt = mysql_query ("select * from products where categoryname = batteries,glasses");

while($apst = mysql_fetch_assoc($trt)) {  

echo '<div style="color: #000; float: left;">Productname: '.$['apst'].'</div>'; 
echo '<br>'; 
}

?>
  • 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-13T17:54:35+00:00Added an answer on June 13, 2026 at 5:54 pm

    You want to use a join table so you can have a many-to-many relationship. Your current structure is better suited to each user only having ONE choice.

    What you want are three tables:

    • Users (userID, user_name, user_email)
    • Categories (categoryID, category_name)
    • Users_Categories (userID, categoryID)

    So to use your example, your three tables would look like this (if anyone knows a better way to demonstrate SQL tables on here, please let me know):

    Users

    • userID: 1 | user_name: George | user_email: george@example.com

    Categories

    • categoryID: 1 | category_name: batteries
    • categoryID: 2 | category_name= flashes
    • categoryID: 3 | category_nameglasses.

    Users_Categories

    • userID: 1 | categoryID: 1
    • userID: 1 | categoryID: 3

    You would then use a select statement with a join clause to get George and his categories:

    SELECT 
        user_name,
        category_name
    FROM
        Users
        LEFT JOIN Users_Categories USING (userID)
        LEFT JOIN Categories USING (categoryID)
    WHERE
        userID = 1
    

    This would return two rows in the result:

    1. George, Batteries
    2. George, Glasses

    Depending on what you want to do with that data, it may be better to also select the userID.

    If you want a query that will only return one row with all of the information, it gets a little trickier. You have to use the GROUP BY functionality. That would look like this:

    SELECT 
        user_name,
        GROUP_CONCAT (category_name ',')
    FROM
        Users
        LEFT JOIN Users_Categories USING (userID)
        LEFT JOIN Categories USING (categoryID)
    GROUP BY
        userID
    WHERE
        userID = 1
    

    For your final question about products, you would follow the same logic with more joins. Your Products table wants to have a productID, product_name, and categoryID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I have a oracle database table which has name value pairs. A set
Hey folks, I have a field within a table that I need to do
Folks, I have a custom WebPart (the ASP.Net variety) that has a property whose
Morning folks, I have a temporary table with 135,000 rows and 24 columns, of
Helllo folks, I have one table with entries, those entries have a field confidentiality
I have a table that records when a user logs in. I want to
Folks, we have the following problem: we've got several objects containing table data that
Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses
I have seen several places in stackoverflow where folks have elegantly and easily turned
Folks I have a sealed class as follows. I want to extend this sealed

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.