kevincro Posted October 19, 2007 Share Posted October 19, 2007 This script populates a select box with MySQL data. The problem I'm having is that it only displays the first result. Does anyone know of a better way to populate a select box with MySQL data? echo "<TD>".'<select name="wgr" >'; echo '<option value="">Applicants</option>'; $conn = mysql_connect(host, database user, password) or die($msg_no_connect); mysql_select_db(database, $conn) or die("Error connect: ".mysql_error()); $result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view "); while ($row=mysql_fetch_assoc($result)) { $sn = $row['column']; $nn = $row['column 2']; echo "<option value='$sn'>$sn-$nn</option>".'</select>'."</td>"; } Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/ Share on other sites More sharing options...
trq Posted October 19, 2007 Share Posted October 19, 2007 Maybe not related but, an array index cannot contain a space. ie; $nn = $row['column 2']; Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372826 Share on other sites More sharing options...
Wes1890 Posted October 19, 2007 Share Posted October 19, 2007 You ended your SELECT inside your while loop.. Try this: echo "<TD><select name='wgr' >"; echo '<option value="">Applicants</option>'; $conn = mysql_connect(host, database user, password) or die($msg_no_connect); mysql_select_db(database, $conn) or die("Error connect: ".mysql_error()); $result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view "); while ($row=mysql_fetch_assoc($result)) { $sn = $row['column']; $nn = $row['column 2']; echo "<option value={$sn}>{$sn}-{$nn}</option>"; } echo "</select></td>"; And here is a cleaner version: (the code below is the same as i posted above.. just easier to read) // Get data $conn = mysql_connect(host, database user, password) or die($msg_no_connect); mysql_select_db(database, $conn) or die("Error connect: ".mysql_error()); $result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view "); echo " <TD> <select name='wgr' > <option value=''>Applicants</option> "; while ($row=mysql_fetch_assoc($result)) { $sn = $row['column']; $nn = $row['column 2']; echo "<option value={$sn}>{$sn}-{$nn}</option>"; } echo " </select> </td>"; Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372827 Share on other sites More sharing options...
cooldude832 Posted October 19, 2007 Share Posted October 19, 2007 also the * operator is not good if you only using 2 fields. I'm assuming u got more than 2 fields in the table. Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372828 Share on other sites More sharing options...
btherl Posted October 19, 2007 Share Posted October 19, 2007 Maybe not related but, an array index cannot contain a space. ie; $nn = $row['column 2']; Actually it can.. I even use UTF8 encoded foreign language strings as array indices. Plenty of them have spaces inside. An associative array index can have anything a string can have. Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372833 Share on other sites More sharing options...
peterbarone Posted October 19, 2007 Share Posted October 19, 2007 maybe this will help $whatever = mysql_query("SELECT something FROM sometable;"); // Then, where you want your drop down list echo "<FORM NAME=\"select\" ACTION=\"select.php\" METHOD=POST>\n"; echo "<font class=\"text1\">What to select:</font> <SELECT CLASS = \"small\" NAME = selected>"; for ($j=0; $j<mysql_num_rows($whatever); $j++){ $list = mysql_fetch_array($whatever); echo "<OPTION VALUE=$list[somevalue]> $list[showsomename] \n";} echo "</SELECT>"; echo "<INPUT CLASS=\"button\" TYPE=submit VALUE='GO!'>\n"; echo "</form>\n"; Hope that can help Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372840 Share on other sites More sharing options...
trq Posted October 19, 2007 Share Posted October 19, 2007 Maybe not related but, an array index cannot contain a space. ie; $nn = $row['column 2']; Actually it can.. I even use UTF8 encoded foreign language strings as array indices. Plenty of them have spaces inside. An associative array index can have anything a string can have. Well I'll be. I guess this is just one of those things I never thought about using. Seems logical now that I think about it. I meen, an associative array's index is simply a string. Still, doesn't look right somehow. But yeah... learn something new. Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372844 Share on other sites More sharing options...
cooldude832 Posted October 19, 2007 Share Posted October 19, 2007 well a space is a defined character right? so it makes sense Link to comment https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/#findComment-372891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.