Jump to content

mysql select help


c_pattle

Recommended Posts

Hey

 

I have two tables called product_list and users_cart.  I am trying to use a select function to retrieve data from both table and then display it as the users shopping cart.  I have used the following sql. 

 

select product_list.product_name, product_list.image, users_cart.* from product_list, users_cart where users_cart.product1 = product_list.product_name and username="chris";

 

However the problem with this is that it only returns the value in product_list.image for the first item in the users shopping cart (users_cart.product1).  Does anyone know how I am get it to return the image for each of the items in the shopping cart (product1, product2 and product3)? 

 

Feel free to ask more question if I haven't explained it well enough. 

 

Thanks for any help

Link to comment
Share on other sites

select product_list.product_name, product_list.image, users_cart.*

FROM product_list

JOIN users_cart

ON (users_cart.product1 = product_list.product_name)

WHERE username="chris";

 

If you are looking to return multiple rows from one table and combine it with a single row from another table, you are better off using multiple queries.

 

If you are looking to return a bunch of values regardless of username try this:

 

select product_list.product_name, product_list.image, users_cart.* 
FROM users_cart
LEFT JOIN product_list
ON (product_list.product_name = users_cart.product1)
WHERE username="chris";

 

Link to comment
Share on other sites

Umm, well, assuming that you made a field for every single thing they could add to their cart (product1, product2, productn . . .), I would recommend that you grab everything with MySQL then manually compare them in PHP. This is the only way to iterate like that.

 

I actually recommend that you make the user's shopping cart something like...

 

id | userid | cart

where cart is filled with ids separated by a |, so...

cart = 1|5|3290|44|7 and so on

 

this way you can get "cart" and explode() it into a list of ids, which you can link up with an inventory. Do you understand what I mean?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.