0

I want to display a table from my database in a dropdown menu. i already made the connection, did the query but nothing is appearing in the dropdown. can anyone tell me why.

below is my code:

<?php 
    include ('db_connect.php');
    $sql ="select ins_name from institution";
    $result = mysqli_query($conn,$sql);                         
    echo "<select name='ins_name'>";
    while ($row = mysql_fetch_array($result)){
    echo "<option value='".$row['ins_name']."'>"."</option>";
    }
    echo "</select>"
?>
ash_dev15
  • 45
  • 5

3 Answers3

0

First, you mixed mysql_* with mysqli_* extension. It would be advisable to just use mysqli_* rather than the deprecated mysql_*.

Assuming that you establish your connection to your database using mysqli_* extension (db_connect.php):

$conn = new mysqli("Host", "Username", "Password", "Database"); /* REPLACE NECESSARY PARAMETERS */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Then your main file:

<?php 

    include ('db_connect.php');

    echo '<select name="ins_name">';

    $stmt = $con->prepare("SELECT ins_name FROM institution"); /* PREPARE QUERY */
    $stmt->execute(); /* EXECUTE QUERY */
    $stmt->bind_result($insname); /* BIND RESULT TO THIS VARIABLE */
    while($stmt->fetch()){ /* GET ALL RESULT */

        echo '<option value="'.$insname.'">'.$insname.'</option>'; 

    } /* END OF WHILE LOOP */
    $stmt->close(); /* CLOSE STATEMENT */

    echo '</select>';

?>

And also, you did not set a data to display inside your <option> in your example. You can see above how we concat the value in your <option></option>

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
0

Use mysqli_fetch_array() and You need to write name between option tag.

For e.g <option value="1">Test</option>

<?php 
    include ('db_connect.php');
    $sql ="select ins_name from institution";
    $result = mysqli_query($conn,$sql);                         
    echo "<select name='ins_name'>";
    while ($row = mysqli_fetch_array($result)){
    echo "<option value='".$row['ins_name']."'>".$row['ins_name']."</option>";
    }
    echo "</select>"
?>
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

You need to use mysqli. Olso you have to display the value in the option,

<?php 
    include ('db_connect.php');
    $sql = "select ins_name from institution";
    $result = mysqli_query($conn,$sql);                         
    echo "<select name='ins_name'>";
    while ($row = mysqli_fetch_array($result)){
        $insName =  $row['ins_name'];
        echo "<option value='".$insName."'>".$insName."</option>";
    }
    echo "</select>"
?>
Rafael Shkembi
  • 786
  • 6
  • 16