Jump to content

[SOLVED] select db fields with explode


timmah1

Recommended Posts

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

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

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 :P

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>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.