I am working on a small form that retrieves and stores values to a DB. I want to retrieve the contents of a table and use it in a drop down box. The code I have used is:
<?php // Connect to the db. require ('mysqli_connect.php'); // Make the query: $q = "SELECT employee_name from employee where dept_id=3 ORDER BY employee_id ASC"; // Run the query. $r = mysqli_query ($dbc, $q); if ($r) // If it ran OK, display the records. { echo '<select name="employee_name">'; // Fetch and print all the records: while ($row = mysqli_fetch_array($r)) { echo '<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>'; } echo "</select>"; } mysqli_free_result ($r); // Free up the resources. mysqli_close($dbc); // Close the database connection. ?>
The code works perfectly in MySQL console. But, it returns an empty drop down box in the webpage. Guys, please help.