0

I'm trying to create a dropdown list of my product type from the database, I'm using this code from How to populate HTML dropdown list with values from database.

<select name="product_type">
<?php 
include('include/conn.php');
$sql = mysql_query("SELECT product_type FROM produk2");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"product_type1\">" . $row['product_type'] . "</option>";
}

?>
</select>

I'm going to add this in my search engine, but the dropdown from that code making it read all data from product_type. Is it possible to create the dropdown without the same product_type twice?

4
  • 4
    Add DISTINCT to the query? Commented Sep 9, 2015 at 3:07
  • 1
    You are looking for distinct result. You could use DISTINCT or GROUP BY. By the way, you should at least use mysqli or even better, PDO. Commented Sep 9, 2015 at 3:08
  • try this SELECT DISTINCT product_type FROM produk2 Commented Sep 9, 2015 at 3:10
  • Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. This article will help you decide. Commented Sep 9, 2015 at 4:43

1 Answer 1

1

You should at least connect using mysqli :

conn.php =

 $con=mysqli_connect("server","username","password","database");
            // Check connection
            if (mysqli_connect_errno())
            {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

Then your query file :

   <select name="product_type">
<?php 
include('include/conn.php');
$sql = mysqli_query($con,"SELECT product_type FROM produk2 GROUP BY product_type")
while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"product_type1\">" . $row['product_type'] . "</option>";
}

?>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

im just starting to understand mysql and now it is really hard for me to try and change to use mysqli,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.