组合框中的数据用数据库中的数据填充

时间:2016-04-01 14:25:53

标签: php database

function dropDownStudent()
{
    $connect = connect();
    $sql = "SELECT lastname, firstname, middleinitial FROM student";
    $sql_connect = mysqli_query($connect, $sql);
}

<?php
include("process.php");

&GT;

<?php
            $result = dropDownStudent();
            while($row=mysqli_fetch_array($result, MYSQL_ASSOC)){?>
            <select name = "stud" required>
                <? echo "<option> value='".$row['id']"'>" . $row['lastname'] . "," . $row['firstname'] . " " . $row['middleinitial'] . "</option>";?>
            </select>
            <?php
            }

&GT;

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,在第11行的C:\ xampp \ htdocs \ capstone \ registerstudent.php中给出null

     

我不知道为什么这是我的错误。我不知道我的参数1是空的

1 个答案:

答案 0 :(得分:0)

在你的函数中你需要返回结果。否则$result将为null,因此mysqli_fetch_array将无效。

function dropDownStudent()
{
    $connect = connect();
    $sql = "SELECT lastname, firstname, middleinitial FROM student";
    return  mysqli_query($connect, $sql); // You did this wrong
}

为了进一步改进它,你不应该在函数中连接:

$connect = connect();
function dropDownStudent()
{
    global $connect;
    $sql = "SELECT lastname, firstname, middleinitial FROM student";
    return  mysqli_query($connect, $sql); // You did this wrong
}

否则,每次使用该功能时都会打开一个新连接。这样,它一次只能连接一个。