Jump to content

Help me please


nasirr

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
Link to comment
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]

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

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.