gpt4 book ai didi

php - MySQL:多重查询不执行第二个和第三个附加查询

转载 作者:行者123 更新时间:2023-11-29 11:36:07 24 4
gpt4 key购买 nike

因此,我尝试发送一个长附加 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']

最佳答案

  1. 不要使用多重查询。一一运行您的查询。
  2. 始终让 mysqli 报告其错误。询问人们你的数据库出了什么问题是没有用的——他们不知道。只有您的数据库可以确定地告诉您。

所以让你的代码这样

$queries = ["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))"];

$queries[] = "INSERT INTO TempCourseProfile(CourseID, CourseName, CourseDescription, CourseObjectives)
SELECT CourseID, CourseName, CourseDescription, CourseObjectives FROM Course WHERE CourseID = $courseID;";

$queries[] = "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;";

$queries[] = "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;";

foreach ($queries as $query) {
$conn->query($query) or trigger_error($conn->error);
}

此代码不会回显任何无用的垃圾邮件,但一旦发生就会报告确切的错误。

关于php - MySQL:多重查询不执行第二个和第三个附加查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com