如何将php数据转换为html select?

时间:2018-11-28 02:59:54

标签: php html

我知道这个问题涉及多个S.O。论坛,但经过审查后,我似乎无法使phpmyadmin中的数据元素出现在我的html select中。我是php的新手,因此我对基础知识不太好。谁能看一下我的代码并告诉我它怎么了?我没有收到任何错误,但是在我的html select中什么也没有。

代码:

<?php

$con = mysqli_connect('localhost','root','','Lab2_Database');

if($con-> connect_error) {
die("Connection Failed:".$con-> connect_error);
}


?>
<h1 id="header">Welcome to The Flight Club WebSite</h1>
<br>
<p>Select a Flight by Flight Number:</p>
<form>
<select> 
<option value="0">Flight Number</option>
    <?php
    $sql = "SELECT flightNumber FROM Flight";
    $result = $con-> query($sql);
        while($row = mysql_fetch_assoc($get))
        {
        ?>
        <option value = "<?php echo($row['flightNumber'])?>" >
            <?php echo($row['flightNumber']) ?>
        </option>
        <?php
        }               
    ?>
</select>
</form>


</body>
 </html>

2 个答案:

答案 0 :(得分:0)

 $result = $con-> query($sql);
        while($row = mysql_fetch_assoc($get))
        {
        ?>
        <option value = "<?php echo($row['flightNumber'])?>" >
            <?php echo($row['flightNumber']) ?>
        </option>
        <?php
        }

更改

$result = $con-> query($sql);
        while($row = mysql_fetch_assoc($result))
        {
        ?>
        <option value = "<?php echo($row['flightNumber'])?>" >
            <?php echo($row['flightNumber']) ?>
        </option>
        <?php
        }
我认为未定义

变量 $ get

答案 1 :(得分:0)

您将mysqli与mysqli对象一起使用

$result = $con-> query($sql);
    while($row = mysql_fetch_assoc($get))
    {
    ?>

将此更改为

$result = $con-> query($sql);
    while($row = $result->fetch_assoc())
    {
    ?>

http://php.net/manual/en/mysqli-result.fetch-assoc.php