所以我试图发送一个长的附加sql查询,它将填充一个临时表。根据表格,我填充了一个存储表格中不同值的数组。
第一个insert语句运行完全正常,并正确插入到数组中。接下来的两个附加查询似乎没有填充数组。
在做了一些错误检查之后,我已经验证了语句在查询末尾的确具有正确的值。
注意:用户名,密码和数据库已从代码中删除
非常感谢任何和所有帮助。
<?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']
答案 0 :(得分:1)
所以以这种方式编写代码
$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);
}
此代码不会回复任何无用的垃圾邮件,但一旦发生就会报告确切的错误。
答案 1 :(得分:0)
使用multi_query的原因非常简单。我们要限制与数据库的通信-单个请求比1000个请求一个接一个地快得多。
multi_query的替代方法是单个查询中有多个记录。
INSERT INTO .. (col1, col2) VALUES (v1.1,v2.1), (v1.2, v2.2), (v1.3, v2.3) ...;
但是所有数据中的大多数都应在插入查询之前进行验证。 许多面向对象的数据模型都通过缓存表列和类型,根据实际的数据库结构为您完成此操作
DESCRIBE table;
在执行任何查询之前,插入/更新之前的数据会受到特定列类型和格式的损害。