PHP mysql查询最后添加引号

时间:2019-05-23 09:24:05

标签: php mysql

我已经这样设置查询:

$query = 'SELECT SGC.sys_id, TBL.semester, SGC.bonus, SGC.exam, SGC.ca FROM SubjectGradeComponent AS SGC, ';
$query .= '(SELECT `sys_id`, `semester` FROM AcademicYearTerm AS AYT, SubjectYearTermLevel AS SYTL WHERE academic_year = "' . $academic_year . '" AND SYTL.subject_id = ' . $subject_id . ' AND SYTL.form_level = ' . $form_level. ' AND SYTL.yearTerm_id = AYT.yearTerm_id) AS TBL ';
$query .= 'WHERE SGC.sys_id = TBL.sys_id;';

但是,当我运行查询时,$mysql->query($query);会返回具有0行的空结果。在phpmyadmin上运行相同的查询将显示所需的结果。我环顾四周,但不明白问题所在。

$mysql->error也不显示任何错误消息

编辑:

生成的查询是这样的:

SELECT SGC.sys_id, TBL.semester, SGC.bonus, SGC.exam, SGC.ca FROM SubjectGradeComponent AS SGC, (SELECT `sys_id`, `semester` FROM AcademicYearTerm AS AYT, SubjectYearTermLevel AS SYTL WHERE academic_year = "2018-2019" AND SYTL.subject_id = 1 AND SYTL.form_level = 1 AND SYTL.yearTerm_id = AYT.yearTerm_id) AS TBL WHERE SGC.sys_id = TBL.sys_id;""

问题是“”来自哪里?

1 个答案:

答案 0 :(得分:1)

好像您想要一个JOIN查询。

您还应该使用带占位符?的预处理语句,而不是将值直接注入查询中。

$query = "SELECT SGC.sys_id, 
                 AYT.semester, 
                 SGC.bonus, 
                 SGC.exam, 
                 SGC.ca 
          FROM SubjectGradeComponent AS SGC
          JOIN AcademicYearTerm AS AYT
              ON SGC.sys_id = AYT.sys_id
          JOIN SubjectYearTermLevel AS SYTL 
              ON SYTL.yearTerm_id = AYT.yearTerm_id
          WHERE academic_year = ? 
            AND SYTL.subject_id = ? 
            AND SYTL.form_level = ?";