我希望$teacherIDList
在使用相同的courseID循环时包含teacherID,例如我希望它为('RITC01','RITC01')
但目前只有('RITC01')
。
PHP:
function getTeacherID($subjectID) {
require ('dbconnect.php');
$teacherIDSQL = mysqli_query($dbc,"SELECT teacherID FROM TeacherClassRoomCourse WHERE courseID = '$subjectID'");
$teacherIDList = [];
while($teacherID = mysqli_fetch_array($teacherIDSQL,MYSQLI_ASSOC)) {
array_push($teacherIDList, $teacherID['teacherID']);
return $teacherIDList;
}
}
getTeacherID('COMP12');
dbconnect.php包含连接到数据库的$dbc
。
TeacherClassRoomCourse表:
答案 0 :(得分:1)
您需要做的就是像在此
那样移动您在while循环之外构建的数组的返回function getTeacherID($subjectID) {
require ('dbconnect.php');
$teacherIDSQL = mysqli_query($dbc,"SELECT teacherID FROM TeacherClassRoomCourse WHERE courseID = '$subjectID'");
$teacherIDList = [];
while($teacherID = mysqli_fetch_array($teacherIDSQL,MYSQLI_ASSOC)) {
array_push($teacherIDList, $teacherID['teacherID']);
}
return $teacherIDList;
}
$someting = getTeacherID('COMP12');
答案 1 :(得分:1)
你return
循环中的while
,所以它在第一次迭代后就会中断。将return
移到循环外部,以便它可以遍历所有结果,而不仅仅是第一个结果。
function getTeacherID($subjectID) {
require ('dbconnect.php');
$teacherIDSQL = mysqli_query($dbc,"SELECT teacherID FROM TeacherClassRoomCourse WHERE courseID = '$subjectID'");
$teacherIDList = [];
while($teacherID = mysqli_fetch_array($teacherIDSQL,MYSQLI_ASSOC)) {
array_push($teacherIDList, $teacherID['teacherID']);
}
return $teacherIDList; // Return after while is complete
}
注意:
getTeacherID()
做任何事情,您需要将其分配给变量$results = getTeacherID('COMP12');