Jump to content


Photo

Help me please


  • Please log in to reply
3 replies to this topic

#1 nasirr

nasirr
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 April 2006 - 08:11 PM

<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>

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

#2 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 11 April 2006 - 08:37 PM

[!--quoteo(post=363779:date=Apr 11 2006, 04:11 PM:name=Nasir)--][div class=\'quotetop\']QUOTE(Nasir @ Apr 11 2006, 04:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<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>

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.

while($nt=mysql_fetch_row($result))
       {
echo '<option value="'. $nt['id']. '">"' .$nt['wht'] ."</option>\n";
}
echo '</select>';



#3 nasirr

nasirr
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 April 2006 - 09:12 PM

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

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 April 2006 - 09:15 AM

change mysql_fetch_row to mysql_fetch_array 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:
while($nt = mysql_fetch_array($result))
{
    echo '<option value="'. $nt['id']. '">"' .$nt['wht'] ."</option>\n";
}
echo '</select>';





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users