Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    no duplicates my_sql_fetch

    Hi got a database filled with products. They are sorted by a product category.

    Code:
    category1, product1
    category1, product2
    category1, product3
    
    category2, product1
    category2, product2
    category2, product3
    Now all I want to do is loop through the records and echo out all product category's there are. Problem is that it echo's out all listed category's there are in the database instead of all unique category's.... That make sence?

    Anyone know how I would set up my query to do such a thing? Here is my current query and mysql_fetch_array.

    PHP Code:
        <?php 
        
    include("db_cnt.php");
        
    $result mysql_query("SELECT * FROM site_products"
        or die(
    mysql_error()); 
        
        while(
    $row mysql_fetch_array$result )) {
        
    $product_category $row['product_category'];
        
        echo 
    "<option value='" $product_category "' >"
        echo 
    $product_category;
        echo 
    "</option>";

        }         
        
    ?>

  • #2
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Ahh NM figured it out.


    PHP Code:
        <?php 
        
    include("db_cnt.php");
        
    $result mysql_query("SELECT product_category FROM site_products GROUP BY product_category"
        or die(
    mysql_error()); 
        
        while(
    $row mysql_fetch_array$result )) {
        
    $product_category $row['product_category'];
        
        echo 
    "<option value='" $product_category "' >"
        echo 
    $product_category;
        echo 
    "</option>";

        }         
        
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •