尝试使用php设置可变长度下拉菜单并从mysql中提取值

尝试使用php设置可变长度下拉菜单并从mysql中提取值,php,mysql,Php,Mysql,我试图制作一个下拉列表,从mySql表中提取可以添加到其他位置的值。当我向列表中添加值时,它会增加下拉菜单的选项数量,但是没有显示文本,只是空插槽 试试这个 <?php $exList = "select * from $exerciseTable"; if($stmt = $conn->prepare($exList)) { $stmt->execute(); $stmt->bind_result($id,$n

我试图制作一个下拉列表,从mySql表中提取可以添加到其他位置的值。当我向列表中添加值时,它会增加下拉菜单的选项数量,但是没有显示文本,只是空插槽

试试这个

<?php 
    $exList = "select * from $exerciseTable";
    if($stmt = $conn->prepare($exList))
    {
        $stmt->execute();

        $stmt->bind_result($id,$name);

        while ($stmt->fetch())
        {

            echo "<option value=$id>$name</option>";
        }
    }


?>

您必须明确指定需要选择的列
id、name
以及以后绑定的列,因为
*
选择所有列,因此MySQLi无法知道要绑定到哪些列
id
name

<?php 
        $exList = "select * from $exerciseTable";
        if($stmt = $conn->prepare($exList))
        {
            $stmt->execute();

        $stmt->bind_result($id,$name);

        while ($stmt->fetch())
        {

            echo "<option value='".$id."'>$name</option>";
        }
    }


?>
那就行了

有关绑定结果用法的更多信息,请查看。



请解释您的答案$id变量没有用引号括起来,因此它的值没有被回显。请将它添加到您的答案中,以便将来阅读它的人知道它没有任何明显的变化。@Manashvibirla不,如果里面没有空格,您可以得到该值values@dlrdlrdlr如果答案对你有用,那就接受它!!抱歉,有计时器,我必须等待几分钟,它才会让我单击“接受”
$exList = "select id, name from $exerciseTable";
<select name="DROPDOWN_NAME">
<?php 
    $exList = "select `id`, `name` from $exerciseTable";
    if($stmt = $conn->prepare($exList))
    {
        $stmt->execute();

        $stmt->bind_result($id,$name);

        while ($stmt->fetch())
        {

            echo "<option value='$id'>$name</option>";
        }
    }


?>
</select>