0

I have a form with a dropdown field which I would like to populate with a list of bands that exist in my 'bands' database. I have tried several pieces of code, but the dropdown list is always empty. I know that the DB connection is fine because I am calling the connection in several parts of my application. Here is my attempt at the code:

<?php

$select_query= "Select bandname from bands";
$select_query_run = mysql_query($select_query);
echo "<select name='bands'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{
        echo "<option value='' >".htmlspecialchars($select_query_array["bandname"])."</option>";
}
echo "</select>";

?>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • check your table name and column name in database. also die if error like `$select_query_run = mysql_query($select_query) or die(mysql_error());` – Satish Sharma Jul 19 '14 at 10:51
  • did you try debugging it? `while(...) {var_dump($select_query_array);}` – tillz Jul 19 '14 at 10:53
  • I'm now getting an error stating that 'No database is selected'. Whilst I understand the context of this message, my database connection is solid as I am using it throughout the application. It's called by default in every script that I create. Does the SELECT query require its own DB connection parameters? – Dean Mark Anthony Jul 19 '14 at 10:57
  • @DeanMarkAnthony Not Necessary. [mysql_select_db](http://php.net/manual/en/function.mysql-select-db.php) Select the DB where you want to get a result from a table. – Gunaseelan Jul 19 '14 at 11:00

2 Answers2

0

Adding mysql_select_db will resolve your issue

<?php
   // Report all PHP errors (see changelog)
   error_reporting(E_ALL);
   mysql_select_db("dbname") or die("Could not open the db");
   $select_query= "select bandname from bands";
   $select_query_run = mysql_query($select_query) or die(mysql_error();
   echo "<select name='bands'>";
    while ($select_query_array=   mysql_fetch_array($select_query_run) )
    {
        echo "<option value='' >".htmlspecialchars($select_query_array['bandname'])."</option>";
    }
    echo "</select>";
?>
Gunaseelan
  • 2,494
  • 5
  • 36
  • 43
0

First of all turn on error reporting and resolve if there is any error.

If there is no error and still no options are displayed, that means the query is not returning any result. So try to confirm it by running the query in another MySQL client and see if it actually returns any result.

Community
  • 1
  • 1
Salman
  • 3,761
  • 2
  • 23
  • 34