简体   繁体   中英

How Do I Grab Information from MySQL using information from a session

I've set up a really simple shopping cart that I've built and I am having trouble using session information to access a MySQL database.

I'm using sessions to store the id and quantity for each product that a customer selects, all information that is sent through a form using $_POST . All that works fine. The snag comes when I then try to take that ID # and access my products table in the MySQL db. I am just unsure of how to call this information out and loop it.

Could you help me out with some possible strategies that I could pursue in order to accomplish my goal?

Thanks so much in advance!

Could you help me out with some possible strategies that I could pursue in order to accomplish my goal?

Read the PHP manual on how to 'talk' to MySQL.

$productId = $_SESSION['productId'];
$sql = " SELECT * FROM products WHERE productId = '$productId'";

You can check the PHP manual to find out how you can perform that query, and then loop through the results. Or just google for a php/mysql tutorial.

If you have a list of IDs, you have some choices.

If the IDs are only stored in an array in a session variable, eg your users cant save them for later. You need to do more work but not a lot.

select <stuff> from products where productID in ( 22,56,3683)

if you have them in a database you could do

select <stuff> from products where productID in (select productID from baskets where userID=34)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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