Trying to Populate Drop Down Menu in Html/php from data in mysql table


I have a program I am working on where I am trying to populate a drop down menu with data from a mysql database. I get the dropdown menu but it has no values in it. Here is the relevant code:

 $records = mysql_query("SELECT * FROM country ");
 while ($row = mysql_fetch_array($records)){
 echo "<option value=\"\">" . $row['countryname'] . "</option>";

The first thing you need to do is STOP using the dangerous and obsolete mysql_* code. It has been completely removed from PHP.

I have tried this new code with same result:\

$query = "SELECT * FROM country";
$result = $mysqli->query($query);

 while($row = $result->fetch_array())
 $rows[] = $row;

 foreach($rows as $row)
 echo $row['countryname'];

 /* free result set */

 /* close connection */


What does the ‘view source’ of the page show?

Do you ask for errors anywhere?

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // top of the script

Also you should explicitly name all columns in the SELECT, and not use * - then you will get definitive errors when accessing unknown columns, instead of just outputting some blank values later.

I solved the problem

Sponsor our Newsletter | Privacy Policy | Terms of Service