1

所以我试图发送一个长附加的 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']

4

2 回答 2

1
  1. 不要使用 multi_query。一个一个地运行您的查询。
  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);
}

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

于 2016-04-12T04:13:48.143 回答
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;

在执行任何查询之前,插入/更新之前的数据会受到特定列类型和格式的影响。

于 2018-11-24T21:09:21.280 回答