mclamais Posted June 19, 2008 Share Posted June 19, 2008 //Example value for p: ?p=12345_1,A2345_1,B3456_2 if(isset($_GET["p"])){ $p = $_GET["p"] } $items = array(); foreach (explode(",", $p) as $input) { list($sku, $quantity) = explode("_", trim($input) { } $skulist = implode(",", array_keys($items)); $query1 = "SELECT product_name, price FROM products WHERE sku IN(".$skulist.")"; This works fine for me if the sku values are numeric, but if alpha the sql fails. Can some tell me the best method for adding single quotes around each value in the array or list. So rather than sending this 12345,A2345,B3456 I need to sent '12345','A2345','B3456' to the query. Thanks, Marc Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/ Share on other sites More sharing options...
0perator Posted June 19, 2008 Share Posted June 19, 2008 you could try escaping them with backslashes, that means php ignores the quotes and just has them as part of a string example: $thomas = 'Thomas'; that is Thomas; $thomas = '\'Thomas\''; that is 'Thomas' enjoy. Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/#findComment-568910 Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 By the way, right here: list($sku, $quantity) = explode("_", trim($input) { There is no ending ) for the explode (and I don't know what the last { is for), like so: list($sku, $quantity) = explode("_", trim($input)); And at the bottom where it says: $skulist = implode(",", array_keys($items)); All $items is is "array();". Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/#findComment-568919 Share on other sites More sharing options...
mclamais Posted June 19, 2008 Author Share Posted June 19, 2008 <?php require_once('conn_localhost.php'); mysql_select_db($database) or die('Could not connect: ' . mysql_error()); if(isset($_GET["p"])){ $p = $_GET["p"] } $items = array(); foreach (explode(",", $p) as $input) { list($sku, $quantity) = explode("_", trim($input)); $items[$sku] = $quantity; } $skulist = implode(",", array_keys($items)); $query1 = "SELECT sku, product_name, price, num_of_decals FROM products WHERE sku IN(".$skulist.")"; $result1 = mysql_query($query1); Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/#findComment-568953 Share on other sites More sharing options...
kenrbnsn Posted June 19, 2008 Share Posted June 19, 2008 The OP asked Can some tell me the best method for adding single quotes around each value in the array or list. which non of the other responders answered. The answer is ... change <?php $skulist = implode(",", array_keys($items)); ?> to <?php $skulist = "'" . implode("','", array_keys($items)) . "'"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/#findComment-569023 Share on other sites More sharing options...
mclamais Posted June 19, 2008 Author Share Posted June 19, 2008 Perfect Ken, thanks a million. Marc Link to comment https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/#findComment-569144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.