Jump to content

Populate select box from sql table


mastubbs

Recommended Posts

Hi all,

Im trying to populate a drop down list from a php table but for some reason im getting stuck. The table structure is:

Table name: Par1AddPatients_dynlist_items

Columns: ID, listid, name, value

 

I am trying to populate the list with names and corresponding values from the table, where the list id is ‘1’.

<select name="ward_list">
<?php
$connect = mysql_connect("localhost","username ","password ");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('jasperss_par1pats');
if(!$DB)
{
die("MySQL could not select Database!");
}
$ward_list = mysql_query("SELECT name AND value FROM Par1AddPatients_dynlist_items WHERE listid = '1'");
while ($row = mysql_fetch_array($ward_list)){
echo '<option value="'. $row['value'] .'">'. $row['name'] .'</option>';
}
?>
</select>

At the moment I’m just getting an empty drop-down box. Any ideas where i'm going wrong?

 

Thanks in advance for any help with this,

 

Matt

Link to comment
Share on other sites

As @mac_gyver has said, your select query is wrong.

 

Here is generic SQL syntax of SELECT command to fetch data from MySQL table:

SELECT field1, field2,...fieldN 
FROM table_name
[WHERE Clause]

Read it from manual

Link to comment
Share on other sites

Try this.

<select name="ward_list">
<?php
$connect = mysql_connect("localhost","username ","password ");

if (!$connect) {
    die("MySQL could not connect!");
}

$DB = mysql_select_db('jasperss_par1pats');

if(!$DB) {
    die("MySQL could not select Database!");
}

$ward_list = mysql_query("SELECT name, value FROM Par1AddPatients_dynlist_items WHERE listid = '1'");

while ($row = mysql_fetch_array($ward_list)){
    echo '<option value="'. $row['value'] .'">'. $row['name'] .'</option>';
}

?>
</select>

Side note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated, read this . Learn about prepared statements  instead, and use PDO or MySQLi

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.