Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting Values from database in array format

Member
 
Join Date: Nov 2008
Location: Chennai, India
Posts: 100
#1: Oct 6 '09
I have a table with only one field say p_code. for that field i have around 30 values like MMBF,TRED,TFRE,EDFC,TTRRE ect., to goes on.

I am trying to retrive all the values in the single query like this:
Expand|Select|Wrap|Line Numbers
  1. $sql_query_product = mysql_query("SELECT Product_Code FROM " . TABLE_PRODUCT_CODE . "");    
  2. while($row_pr = mysql_fetch_array($sql_query_product))
  3. {                      
  4. $product_code = array($row_pr['Product_Code']);
  5. }
  6.  
But i m getting the last value of that field.

Can anyone help me how to get all the values in a single array with comma seperated values.

Thanks in advance,

Regards,

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Oct 6 '09

re: Getting Values from database in array format


You're resetting the $product_code variable with each iteration. I recommend pushing the new value into an array like so:

Expand|Select|Wrap|Line Numbers
  1. $product_codes = array();
  2.  
  3. while (/* mysql */) {
  4.     $product_codes[] = // data
  5. }
  6.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#3: Oct 6 '09

re: Getting Values from database in array format


And, once you have them all in an array, if you want that as a comma-separated list, you can use implode.
Expand|Select|Wrap|Line Numbers
  1. $list = implode($product_codes, ",");
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#4: Oct 6 '09

re: Getting Values from database in array format


info: some PHP classes allow you to directly save the result set into an array without further looping…
Member
 
Join Date: Nov 2008
Location: Chennai, India
Posts: 100
#5: Oct 6 '09

re: Getting Values from database in array format


Quote:

Originally Posted by Atli View Post

And, once you have them all in an array, if you want that as a comma-separated list, you can use implode.

Expand|Select|Wrap|Line Numbers
  1. $list = implode($product_codes, ",");

Yeah,

This solved my problem,

Thanks

Regards,
Reply