2016-04-01 168 views
1
function dropDownStudent() 
{ 
    $connect = connect(); 
    $sql = "SELECT lastname, firstname, middleinitial FROM student"; 
    $sql_connect = mysqli_query($connect, $sql); 
} 

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

>数据填充数据在数据库

<?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 
      } 

>

警告:?mysqli_fetch_array()预计参数1被mysqli_result,在C空给出:\ xampp \ htdocs \ capstone \ registerstudent.php上线11

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

回答

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 
} 

否则,你将打开一个新的连接每次你使用该功能。这样一次只有一个连接。