Friday, January 22, 2016

PHP MYSQL: Display Data in Combobox

Hi there! Today I am going to create a database-driven combobox/dropdown menu. I will display all records from the CATEGORY table into a combobox/dropdown menu.

Table

CATEGORY










PHP and MySQL Code

<select>
<?php
$con = mysqli_connect("localhost","root","","sample");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//display values in combobox/dropdown
$result = mysqli_query($con,"SELECT * FROM category ORDER BY category_name");
while($row = mysqli_fetch_assoc($result))
{
echo "<option>$row[category_name]</option>";
    } 
?>
</select>

Insert the above code between your form tag and save it with the file extension of .php. The first six(6) lines in php code is the database connection.

Output










PHP MYSQL: Display Data in Combobox

PHP MYSQL: Display Data in Combobox Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment