timmah1 Posted February 4, 2009 Share Posted February 4, 2009 Is this possible? <?php $products = explode(', ', $product); $q = mysql_query("SELECT * FROM products WHERE id = '$products'"); while ($b = mysql_fetch_array($q)) { echo $b['name']; } ?> Right now, the products field has the information stored with the id numbers separated by commas like so: 45,39,10 etc How can I grab the information with the explode function? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/143729-solved-select-db-fields-with-explode/ Share on other sites More sharing options...
killah Posted February 4, 2009 Share Posted February 4, 2009 $q = mysql_query("SELECT name FROM `products` WHERE `id` IN(".$products.")") or die(mysql_error()); while($b = mysql_fetch_assoc($q)) { echo $b['name'].'<br>'; } Reference => MySQL IN() function. Link to comment https://forums.phpfreaks.com/topic/143729-solved-select-db-fields-with-explode/#findComment-754130 Share on other sites More sharing options...
timmah1 Posted February 4, 2009 Author Share Posted February 4, 2009 Thought I would post the solution <?php $products = explode(', ', $product); foreach($products as $key) { $q = mysql_query("SELECT * FROM products WHERE id = '$key'"); while ($b = mysql_fetch_array($q)) { echo $b['name']."<br />"; } } ?> Sometimes it takes me to post it on here to find the answer Link to comment https://forums.phpfreaks.com/topic/143729-solved-select-db-fields-with-explode/#findComment-754131 Share on other sites More sharing options...
uniflare Posted February 4, 2009 Share Posted February 4, 2009 Sounds to me your a little confused on how to deal with complicated database sctructures, have a look at the mysql tutorials on this site. -- If my assumption is correct, ou want to grab the comma sperated id list from mysql, into an array, like so: For a more direct answer, yes you can use explode. but not like that: like this: <?php // Grab that specific field $q = mysql_query("SELECT `id_field` FROM products WHERE id = '$products'"); // Now loop each result (should only have one im assuming). while ($b = mysql_fetch_array($q)) { // Now each time we go through, we have a new "id_field" filled with id's. // Now we get an array of those id's $products = explode(',', $b['id_field']); // you would have to loop again (maybe a foreach $products As $productID) } ?> This is assuming you have lors of rows, and in each of these rows you have a column that keeps id's of other rows (maybe in other tables)?. IF you want information about those "other" rows, you will first have to grab the id's, sort through them, then make a massive! query (with each id, WHERE `id`='id_1' OR `id`='id_2' .... etc) This looks like a horrible way to sort your database. Maybe if you tell use what your database is to store we can help you make a much easier structure to maintain. ------------------- i see, nm lol, you already made the list of id's Link to comment https://forums.phpfreaks.com/topic/143729-solved-select-db-fields-with-explode/#findComment-754132 Share on other sites More sharing options...
killah Posted February 4, 2009 Share Posted February 4, 2009 Yet again.. i find this much easier: <?php $product = implode(',', $products); $select = mysql_query("SELECT name FROM `products` WHERE `id` IN(".$product.")"); //Reason for only selecting name: //Because why fetch the entire table when we only trying to display one row? //more efficient while ($soc = mysql_fetch_assoc($select)) { echo $soc['name'].'<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/143729-solved-select-db-fields-with-explode/#findComment-754133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.