Jump to content

Populate select box from sql table


Go to solution Solved by thara,

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
https://forums.phpfreaks.com/topic/279933-populate-select-box-from-sql-table/
Share on other sites

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.