Getting Values from database in array format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpmagesh
    New Member
    • Nov 2008
    • 119

    Getting Values from database in array format

    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:
    Code:
    $sql_query_product = mysql_query("SELECT Product_Code FROM " . TABLE_PRODUCT_CODE . "");	
    while($row_pr = mysql_fetch_array($sql_query_product))
    {				      
    $product_code = array($row_pr['Product_Code']);
    }
    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
    Recognized Expert Expert
    • Jun 2007
    • 6092

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

    Code:
    $product_codes = array();
    
    while (/* mysql */) {
        $product_codes[] = // data
    }

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      And, once you have them all in an array, if you want that as a comma-separated list, you can use implode.
      [code=php]$list = implode($produc t_codes, ",");[/code]

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        info: some PHP classes allow you to directly save the result set into an array without further looping…

        Comment

        • phpmagesh
          New Member
          • Nov 2008
          • 119

          #5
          Originally posted by Atli
          And, once you have them all in an array, if you want that as a comma-separated list, you can use implode.
          [code=php]$list = implode($produc t_codes, ",");[/code]
          Yeah,

          This solved my problem,

          Thanks

          Regards,

          Comment

          Working...