所以我试图发送一个长附加的 sql 查询,它将填充一个临时表。基于表,我正在填充一个数组,该数组存储表中的不同值。
第一个插入语句运行得非常好,并正确插入到数组中。接下来的两个附加查询似乎没有填充数组。
在进行了一些错误检查后,我验证了语句在查询末尾确实具有正确的值。
注意:用户名、密码和数据库已从代码中删除
非常感谢任何和所有帮助。
<?php
session_start();
$servername = "localhost";
$username = "";
$password = "";
$db = "";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
$courseID = $_GET['id'];
echo $courseID;
$studentID = $_SESSION['memberID'];
echo $studentID;
$maketemp = "CREATE TEMPORARY TABLE IF NOT EXISTS TempCourseProfile (
CourseID int not null,
CourseName varchar(100),
CourseDescription varchar(200),
CourseObjectives varchar(200),
CourseStartDate date,
CourseEndDate date,
InstructorFirstName varchar(50),
InstructorLastName varchar(50),
PRIMARY KEY(CourseID))";
if($conn->query($maketemp) === TRUE){
echo "you win";
}
else {
echo "you lose";
}
$query = "INSERT INTO TempCourseProfile(CourseID, CourseName, CourseDescription, CourseObjectives)
SELECT CourseID, CourseName, CourseDescription, CourseObjectives FROM Course WHERE CourseID = $courseID;";
$query .= "INSERT INTO TempCourseProfile(CourseStartDate, CourseEndDate)
Select CI.ClassStartDate, CI.ClassEndDate From CourseInSemester CI JOIN ClassSchedule CS on CI.CourseInSemesterID = CS.CourseInSemesterID WHERE CS.StudentID = $studentID AND CI.CourseID = $courseID;";
$query .= "INSERT INTO TempCourseProfile(InstructorFirstName, InstructorLastName)
Select M.FirstName, M.LastName from Member M JOIN CourseInSemester CI ON CI.TeacherID = M.MemberID JOIN ClassSchedule CS ON CS.CourseInSemesterID = CI.CourseInSemesterID WHERE CI.CourseID = $courseID AND CS.StudentID = $studentID;";
echo $query;
if ($conn->multi_query($query)) {
do{
echo "added";
}while($conn->next_result());
} else {
echo "failed to create temp" . $conn->error;
}
$tmparray = array();
$sql = "SELECT * FROM TempCourseProfile where CourseID = $courseID";
//$result = $conn->query($sql);
if($result = $conn->query($sql)){
while ($r = $result->fetch_array()) {
$e = array();
$e['CourseID'] = $r['CourseID'];
$e['CourseName'] = $r['CourseName'];
$e['CourseDescription'] = $r['CourseDescription'];
$e['CourseObjectives'] = $r['CourseObjectives'];
$e['CourseStartDate'] = $r['CourseStartDate'];
$e['CourseEndDate'] = $r['CourseEndDate'];
$e['InstructorFirstName'] = $r['InstructorFirstName'];
$e['InstructorLastName'] = $r['InstructorLastName'];
array_push($tmparray, $e);
}
} else {
echo $conn->error;
}
echo json_encode($tmparray);
?>
该问题必须处理$e['CourseStartDate']
, $e['CourseEndDate']
, $e['InstructorFirstName']
,的值$e['InstructorLastName']