nasirr Posted April 11, 2006 Share Posted April 11, 2006 [code]<tr> <? echo "<td>"; $link = mysql_connect( "localhost", 'root', 'root' ); if ( ! $link ) die( "Couldn't connect to MySQL" ); mysql_select_db( 'project', $link ) or die ( "Couldn't open user: ".mysql_error() ); $sql="SELECT * FROM labels"; $result = mysql_query($sql); echo "<select name=project>"; echo "<option value='1'>-Select Project-</option>";while($nt=mysql_fetch_row($result)) {//echo "<option value=".$nt[id]."></option>";echo "<option value=".$nt["id"].">".$nt["wht"]."</option>";}echo "</select>"; ?></td>[/code]what i m trying to do is that get the values from the database for a drop down menuand i got nothing there is only one option "select project" which is outside from the while statement of fetch intstruction please help me what is the problem with this code Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted April 11, 2006 Share Posted April 11, 2006 [!--quoteo(post=363779:date=Apr 11 2006, 04:11 PM:name=Nasir)--][div class=\'quotetop\']QUOTE(Nasir @ Apr 11 2006, 04:11 PM) [snapback]363779[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]<tr> <? echo "<td>"; $link = mysql_connect( "localhost", 'root', 'root' ); if ( ! $link ) die( "Couldn't connect to MySQL" ); mysql_select_db( 'project', $link ) or die ( "Couldn't open user: ".mysql_error() ); $sql="SELECT * FROM labels"; $result = mysql_query($sql); echo "<select name=project>"; echo "<option value='1'>-Select Project-</option>";while($nt=mysql_fetch_row($result)) {//echo "<option value=".$nt[id]."></option>";echo "<option value=".$nt["id"].">".$nt["wht"]."</option>";}echo "</select>"; ?></td>[/code]what i m trying to do is that get the values from the database for a drop down menuand i got nothing there is only one option "select project" which is outside from the while statement of fetch intstruction please help me what is the problem with this code[/quote]Try this, should work as long as your query is actually returning what it should.[code]while($nt=mysql_fetch_row($result)) {echo '<option value="'. $nt['id']. '">"' .$nt['wht'] ."</option>\n";}echo '</select>';[/code] Quote Link to comment Share on other sites More sharing options...
nasirr Posted April 11, 2006 Author Share Posted April 11, 2006 by using this what it return in the option is just "mean there are two enteries in the table and its return " for bothhelp me please Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted April 12, 2006 Share Posted April 12, 2006 change [b]mysql_fetch_row[/b] to [b]mysql_fetch_array[/b] instead. As mysql_fetch_row doesnt return the results with the column names as the indexes of the array, as currently mysql_fetch_row is returning your results like so:$nt[0], $nt[1]However mysql_fetch_array will retrun the results with the column headers as the array index, ie:$nt['id'], $nt['wht'] etc.So the following should now work:[code]while($nt = mysql_fetch_array($result)){ echo '<option value="'. $nt['id']. '">"' .$nt['wht'] ."</option>\n";}echo '</select>';[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.