简体   繁体   中英

fetch data from database and display in dropdownlist

I am trying to fetch a data from my database and display it in my dropdownlist but i cant get the result I try the other post here but cant understand

anyone can help me with this this is for my project

<?php

    $connection =mysql_connect('localhost', 'root', '','mjj_app');

    $sql="SELECT food FROM menu";
    $lists=mysql_query($sql);

    echo"<select name='fname' id='mySelect' value='Foodname'>"

    while($food = mysql_fetch_array($lists)) {
       echo '<option value='.$food['food'].'>'.$food['food'].'</option>'; 
    }
    echo '</select>';
?>

If you are actually using the mysql_ database extension you also need to select a database using mysql_select_db()

You also need to check that each call the the mysql_ extension has not returned any errors.

<?php

    $connection =mysql_connect('localhost', 'root', '','mjj_app');
    if ( ! $connection ) {
        echo 'Could not connect: ' . mysql_error();
        exit;
    }
    if ( ! mysql_select_db('DATABASE_NAME') ) {
        echo 'Cant select database : ' . mysql_error();
        exit;
    }

    $sql="SELECT food FROM menu";
    $lists=mysql_query($sql);
    if ( ! $lists ) {
        echo 'Query failed: ' . mysql_error();
        exit;
    }

    echo"<select name='fname' id='mySelect' value='Foodname'>"

    while($food = mysql_fetch_array($lists)) {
       echo '<option value='.$food['food'].'>'.$food['food'].'</option>'; 
    }
    echo '</select>';
?>

As you are obviously still learning, spend your time learning the mysqli_ or PDO database extension. The mysql_ extension has been deprecated for years and will totally disapprear in PHP7, out sometime soon, which will make you code obsolete. See this post its a good read and will help you decide which suits you.

You were missing database selecting step, using mysql_select_db

Also it is good practice to catch the possible errors or zero number of records returning from query. Please see below code.

$connection = mysql_connect('localhost', 'root', '');
    if(!$connection) 
    {
        echo 'Could not connect to server because : '.mysql_error();
        exit;
    }

    if(!mysql_select_db('database_name')) 
    {
        echo 'Could not select database because : '.mysql_error();
        exit;
    }

    $sql = "SELECT food FROM menu";
    $lists = mysql_query($sql);

    if($lists)
    {
        if(mysql_num_rows($lists) > 0)
        {
            echo "<select name='fname' id='mySelect' value='Foodname'>";
            while($food = mysql_fetch_array($lists)) 
            {
                echo '<option value='.$food['food'].'>'.$food['food'].'</option>'; 
            }
            echo '</select>';
        }
        else
        {
            echo "No records found.";
        }
    }
    else
    {
        echo "Error while fetching records : ".mysql_error();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM