简体   繁体   中英

populate drop down list from database using mysqli prepared statements

As the title says i want to generated data from database and show it in dropdown. I made a code and it doesn't show any error but the thing is it echos the code in dropdown. It works when i just echo it. And one more thing is it doesn't show DISTINCT results.

Here is my code:

<html>
<head>
<title>FILTER</title>
</head>
<body>
<?php include 'conn.php';?>

<?php
    $stmt = $con->prepare("SELECT DISTINCT author, book_name, language FROM bk_tst_fltr ");
    $stmt->execute();
    $stmt->bind_result($author,$book_name,$language);
    $stmt->store_result();
    echo "<select name='book'>";
    while($row=$stmt->fetch()){?>
        <p><?php echo  '<option value="$row["author"]">"$row["author"]"</option>'; ?></p>
    <?php }
    echo "</select>";
?>

</body>
</html>

Is shows $row["author"] in the dropdown.

Can anyone solve this problem???

Thank You.

My DATABASE

id  author  book_name language price
1   Kishore One       english  500
2   Kumar   two       english  600
3   Kishore three     german   700

You should use double quotes for variable expanding, or exit the string and concat normally.

Also, change to while($stmt->fetch()){?>

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

Try also using this query instead:

"SELECT author, book_name, language FROM bk_tst_fltr GROUP BY author"

The very statement of question is wrong.

Prepared statements has absolutely nothing to do with dropdown lists.

And never have to be.

What you have to populate is an array. Which later have to be used in a template.

So, divide your question into two.

  1. How to select some data into array.
  2. How to output an array in a template.

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