Re: [PHP] mySQL Queries using PHP's SESSION variables
On Thu, Jul 25, 2002 at 02:51:13PM -0400, Anup wrote: > Hello, I am stuck here. In the name of efficiency I want to lower the number > of callls to the database. So I am trying to give the most stringent query > possible. This is the problem: I have stored the surfers shopping cart, > where each item is stored as a session variable.Now on the database I have > ALL inventory items, but I want to only display the details of what the user > has in his cart. > eg. : I want something like this: > > $result = mysql_query("SELECT * from Inventory where ItemNumber is in > $HTTP_SESSION_VARS"); Other folks have made suggestions, but this is way simpler. Assuming the session vars and ItemNumber are numbers: $result = mysql_query('SELECT * from Inventory where ItemNumber IN (' . implode(',', $_SESSION) . ')'); If it's strings: $result = mysql_query("SELECT * from Inventory where ItemNumber IN ('" . implode("','", $_SESSION) . "')"); --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mySQL Queries using PHP's SESSION variables
well, you could simply loop through the session vars and build the query as you go. $query = "SELECT * FROM Inventory WHERE"; // I'm assuming you keep your item numbers in a "items" or something similar foreach ($HTTP_SESSION_VARS['items'] as $pid) $subquery .= " ItemNumber = '$pid' OR "; // get rid of last _OR_ $subquery = ereg_replace('[:space:]OR[:space:]$', '', $subquery); // put the query together $query = $query . $subquery; // add more crap to query if you want $query .= " and some_other_column='something_else'"; Jim Grill Support Web-1 Hosting http://www.web-1hosting.net - Original Message - From: "Anup" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, July 25, 2002 1:51 PM Subject: [PHP] mySQL Queries using PHP's SESSION variables > Hello, I am stuck here. In the name of efficiency I want to lower the number > of callls to the database. So I am trying to give the most stringent query > possible. This is the problem: I have stored the surfers shopping cart, > where each item is stored as a session variable.Now on the database I have > ALL inventory items, but I want to only display the details of what the user > has in his cart. > eg. : I want something like this: > > $result = mysql_query("SELECT * from Inventory where ItemNumber is in > $HTTP_SESSION_VARS"); > // I need proper syntax to replace "is in" > > where Inventory has, ItemNumber (unique), Price, ItemName. > So say the surfer has three items in the Session, then I stored the three > unique ItemNumbers. > Then with the above query I can get the rest of the information to represent > to the user. > I am looking down the avenues of a Set datastyp or maybe Enum, but I don't > know if it will help. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fw: [PHP] mySQL Queries using PHP's SESSION variables
You might be making it more difficult than it needs to me. You're not dealing with a huge number of queries here so why not just loop it? I would simply loop through the $_SESSION array and query the dbase for each item. foreach ($_SESSION as $key => $val) { $query = "SELECT * FROM inventory WHERE itemnumber = '$val'"; $result = mysql_query($query, $db); // .. blah blah blah... } -Kevin - Original Message - From: "Anup" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, July 25, 2002 12:51 PM Subject: [PHP] mySQL Queries using PHP's SESSION variables > Hello, I am stuck here. In the name of efficiency I want to lower the number > of callls to the database. So I am trying to give the most stringent query > possible. This is the problem: I have stored the surfers shopping cart, > where each item is stored as a session variable.Now on the database I have > ALL inventory items, but I want to only display the details of what the user > has in his cart. > eg. : I want something like this: > > $result = mysql_query("SELECT * from Inventory where ItemNumber is in > $HTTP_SESSION_VARS"); > // I need proper syntax to replace "is in" > > where Inventory has, ItemNumber (unique), Price, ItemName. > So say the surfer has three items in the Session, then I stored the three > unique ItemNumbers. > Then with the above query I can get the rest of the information to represent > to the user. > I am looking down the avenues of a Set datastyp or maybe Enum, but I don't > know if it will help. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] mySQL Queries using PHP's SESSION variables
Hello, I am stuck here. In the name of efficiency I want to lower the number of callls to the database. So I am trying to give the most stringent query possible. This is the problem: I have stored the surfers shopping cart, where each item is stored as a session variable.Now on the database I have ALL inventory items, but I want to only display the details of what the user has in his cart. eg. : I want something like this: $result = mysql_query("SELECT * from Inventory where ItemNumber is in $HTTP_SESSION_VARS"); // I need proper syntax to replace "is in" where Inventory has, ItemNumber (unique), Price, ItemName. So say the surfer has three items in the Session, then I stored the three unique ItemNumbers. Then with the above query I can get the rest of the information to represent to the user. I am looking down the avenues of a Set datastyp or maybe Enum, but I don't know if it will help. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php