0

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?

3
  • 1
    have you run the query in db , is that returning the result ? Commented Jan 21, 2014 at 14:58
  • 'mysql_select_db("disertation ", $con); $results = mysql_query("SELECT name FROM user_parent;");' Commented Jan 21, 2014 at 14:59
  • use mysqli instead of mysql recommended Commented Jan 21, 2014 at 15:03

4 Answers 4

3

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());
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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);
?>

Comments

0

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.';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.