2016-04-01 167 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 
} 

否則,你將打開一個新的連接每次你使用該功能。這樣一次只有一個連接。