SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed...Select Listbox empty!

    HI, i am new to PHP and was wondering how to populate a Select listbox from the database, this is what i came up with, but it doesnt seem to work, can someone please help me with this....and there are 2 rows in the database when i run the select statement below, but there seems to be nothing in the box....Help!!!!


    <td><select name="Man_listbox">;

    <?php

    $host = "";
    $userName = "root";
    $password = "";
    $database = "DB";
    mysql_connect($host,$userName,$password) OR DIE("Unable to connect to database");
    @mysql_select_db($database) or die( "Unable to select database");

    $sql = "SELECT Man_Name from manufacturer";
    $result = mysql_query($sql);

    while($menu = mysql_fetch_array($result));
    {
    $menu1= $menu['Man_Name'];
    echo "<OPTION VALUE="$menu1">"$menu1"</OPTION>";
    }
    ?>
    </select>

  2. #2
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    PHP Code:
    echo "<OPTION VALUE=\"$menu1\">$menu1</OPTION>"

  3. #3
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    putting $menu1 in double quotes will just print out $menu1 instead of the value within the variable. Try this:
    PHP Code:
    echo '<option value=' .$menu1 .'>' .$menu1 .'</option>'

  4. #4
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The echo line should be:

    PHP Code:
    echo "<option value=".$menu1.">".$menu1."</option>"
    Or...

    PHP Code:
    echo '<option value="'.$menu1.'">'.$menu1.'</option>'
    For valid html.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Tried it, but it still doesn't have any values in the listbox....is there a problem with my code??

  6. #6
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Echo out the SQL string to see what you are pulling from the database:

    PHP Code:
    echo $sql

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmm....nothing.....is there a problem with the database connection?? But even the echo statements for the non connection of the database is not there!! So i have no idea whats going on....

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while($menu = mysql_fetch_assoc($result));
    {
    $menu1= $menu['Man_Name'];

  9. #9
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try putting

    echo mysql_error();

    after the connection and query parts

    and take the @ off the @mysql_select_db

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Cyberia
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devised
    Echo out the SQL string to see what you are pulling from the database:
    No, "echo $sql;" would only output SQL string which is already set ("SELECT Man_Name from manufacturer").

    You should put:

    PHP Code:
    echo "<pre>"print_r($menu); echo "</pre>"
    in the while() loop to see what you are actually pulling from database.

  11. #11
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, please disregard my echo-the-sql-string reflex mechanism. Thanks for catching that mikis.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •