0% found this document useful (0 votes)
27 views

Databind Dropdown List in PHP

This document provides instructions for creating a dropdown list bound to data from a MySQL database in PHP. It explains that you first connect to the database and query it to retrieve data. Then a select element is added to the page and PHP is used to loop through the database results, outputting option elements with the value coming from one column and the text from another column. This allows the dropdown list to be dynamically populated with options from the database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Databind Dropdown List in PHP

This document provides instructions for creating a dropdown list bound to data from a MySQL database in PHP. It explains that you first connect to the database and query it to retrieve data. Then a select element is added to the page and PHP is used to loop through the database results, outputting option elements with the value coming from one column and the text from another column. This allows the dropdown list to be dynamically populated with options from the database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Databind dropdown list (select) in PHP

Being new to PHP, it may occur to you about how to databind controls. I thought of putting it down here
about how to do it. To start with we create the connection and query the database.

<?php
$link = mysql_connect("localhost","dbuser","password");

if (!$link) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("mydatabase");
$sql = mysql_query("select * from employee");?>

Then add the select statement
<select name="dropdownlist">
<?php
while($row = mysql_fetch_array($sql))
{
?>
<option value=" <?php echo $row['ID']?>">
<?php echo $row['EmpName'] ?></option>
<?php } ?>
</select>

Each row is fetched into $row and the column values can be access by passing the column name like
$row['EmpName'] as in above example, the value of option is assigned from ID and the text is added from
EmpName.

You might also like