简体   繁体   中英

how to populate a drop down list from my SQL database using php

i want to populate a drop down list from my sql database using php, but it shows the below error:

"Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\\wamp\\www\\Q&A\\signup.php on line 43"

my code is:

<?php
$sql = "SELECT category_name FROM category ORDER BY RAND() LIMIT 1";
$result=mysqli_query($conn, $sql) or die ("Query to get data from category failed: ".mysql_error());
    while ($row = mysql_fetch_array($result)) {
    $category_name=$row["category_name"];
    echo "<option>" . $category_name . "</option>";
                }
 ?>

It's because you're using old function mysql_fetch_array it should be mysqli_fetch_array .

Also mysql_error should be mysqli_error which accepts connection as single parameter.

Update your code like this,

<?php
$sql = "SELECT category_name FROM category ORDER BY RAND() LIMIT 1";
$result=mysqli_query($conn, $sql) or die ("Query to get data from category failed: ".mysqli_error(conn));
    while ($row = mysqli_fetch_array($result)) {
    $category_name=$row["category_name"];
    echo "<option>" . $category_name . "</option>";
                }
 ?>

I'm assuming you've a valid connection to the database in $conn variable.

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