Jump to content

Archived

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

nasirr

Help me please

Recommended Posts

[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 menu
and 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

Share this post


Link to post
Share on other sites
[!--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 menu
and 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]

Share this post


Link to post
Share on other sites
by using this what it return in the option is just "
mean there are two enteries in the table
and its return " for both
help me please

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.