BE57
BE57

Reputation: 39

populate dropdown list with database value

I want to populate a drop down list with data from a specific field in the database. Here is my sample code

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("disertation ", $con);
$results = mysql_query("SELECT name FROM user_parent;");
?>

<select name="name">
                    <option value="name">Select one</option>
                    <?php
                         while($row=mysql_fetch_array($results))
                              { echo '<option value=" ' . $row['name'] . ' ">' . $row['name'] . '</option>';  }
                       ?>
               </select>

It's currently displaying nothing from db, any help?

Upvotes: 0

Views: 181

Answers (4)

MrMarlow
MrMarlow

Reputation: 856

To answer your question, directly, you should be first checking if there are any errors (mysql_error()) and then checking there are some results (mysql_num_rows) - these make for easier debugging of your code, since it will tell you what is wrong.

Try this;

<?php
// Connect with user
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
// Select database
mysql_select_db("disertation", $con);

// Run query
$results = mysql_query("SELECT `name` FROM `user_parent`;") or die (mysql_error());
// Check for no results
if (mysql_num_rows($results) == 0)
{
    echo 'There are no options for you to select.';
}
else
{
    // If results, loop them.
    // If the names are user input, make sure they're displayed in non-raw form
    echo '<select name="name">
        <option value="name">Select one</option>';

        while($row = mysql_fetch_assoc($results))
        {
            $name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
            echo '<option value=" ' . $name . ' ">' . $name . '</option>';  
        }

    echo '</select>';
}

Will edit with a mysqli_ solution, if that is an option for you, since mysql_ is deprecated and will be dropped from PHP support, sooner or later.


MySQLi solution;

<?php
// Connect to database;
$mysqli = new mysqli("localhost", "my_user", "my_password", "data_base");

if (mysqli_connect_errno()) 
{
    die("Connect failed: " . mysqli_connect_error());
}

$result = $mysqli->query("SELECT `name` FROM `user_parent`");
if ($result->num_rows > 0)
{
    echo '<select name="name">
        <option value="name">Select one</option>';

        while($row = $result->fetch_assoc)
        {
            $name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
            echo '<option value=" ' . $name . ' ">' . $name . '</option>';  
        }

    echo '</select>';
    $result->close();
}
else
{
    echo 'There are no options for you to select.';
}

Upvotes: 0

Susheel Singh
Susheel Singh

Reputation: 3854

I would recommend you to use mysqli instead of mysql

<?php
    $con=mysqli_connect("localhost","root","","disertation ");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $resource= mysqli_query($con,"SELECT * FROM user_parent");

    echo "<select class="name"><option value="name">Select one</option>";
    while($result = mysqli_fetch_array($resource)){
        echo '<option value="'.$result["name"].'">'.$result["name"].'</option>';
    }
    echo "</select>";

    mysqli_close($con);
?>

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Try this, some white space in your code mysql_select_db("disertation", $con);

 mysql_select_db("disertation", $con);
 $results = mysql_query("SELECT name FROM user_parent") or die (mysql_error());

Upvotes: 3

user3181236
user3181236

Reputation: 357

Make your mysql_fetch_array call read:

mysql_fetch_array($results, MYSQL_ASSOC)

Without MYSQL_ASSOC you can't refer to column names in $row.

Also, consider using MYSQLI or PDO. MYSQL is considerably outdated.

Upvotes: 0

Related Questions