mastubbs Posted July 7, 2013 Share Posted July 7, 2013 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 More sharing options...
mac_gyver Posted July 7, 2013 Share Posted July 7, 2013 in programming, AND is a logical operator. name AND value produces the boolean result of the column value in name AND'ed with the column value in value. to make a list of columns to SELECT, you would separate them with commas , Link to comment https://forums.phpfreaks.com/topic/279933-populate-select-box-from-sql-table/#findComment-1439781 Share on other sites More sharing options...
thara Posted July 7, 2013 Share Posted July 7, 2013 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 https://forums.phpfreaks.com/topic/279933-populate-select-box-from-sql-table/#findComment-1439783 Share on other sites More sharing options...
thara Posted July 7, 2013 Share Posted July 7, 2013 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 https://forums.phpfreaks.com/topic/279933-populate-select-box-from-sql-table/#findComment-1439784 Share on other sites More sharing options...
mastubbs Posted July 7, 2013 Author Share Posted July 7, 2013 Ah ok i see what you mean. Will try to get into using prepared statements. Thanks for the help. Matt Link to comment https://forums.phpfreaks.com/topic/279933-populate-select-box-from-sql-table/#findComment-1439799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.