嵌套quires上的命令不同步错误

时间:2016-08-26 08:09:51

标签: mysql sql phpmyadmin

我直接在PHPMyAdmin中使用SQL标签来执行我的查询,但仍然得到相同的错误Command out of sync error这是我正在尝试执行的MySQL查询:

SELECT `courses`.`id`               as `courseId`, 
       `courses`.`Name`             as `courseName`, 
       `course2teacher`.`TeacherId` as `teacherId`, 
       (SELECT user.name 
        FROM   user, 
               (SELECT course2teacher.TeacherId as tid 
                FROM   course2teacher, 
                       user, 
                       follows, 
                       teachers 
                WHERE  user.id = 10 
                       AND user.id = follows.studentid 
                       AND course2teacher.id = follows.course2teacherid) as 
               teach 
        WHERE  teach.tid = user.id) as teacherName, 
       `assignments`.`Assignment#`  AS `assignmentName`, 
       `assignments`.`DueDate`      AS `duedate`, 
       `assignments`.`DueTime`      AS `duetime`, 
       `assignments`.`expiryDate`   AS `expiryDate` 
FROM   `teachers`, 
       `courses`, 
       `assignments`, 
       `course2teacher`, 
       `user`,
       `follows`
WHERE  `user`.`id` = '10' 
       AND `user`.`id` = `follows`.`studentid` 
       AND `follows`.`course2teacherid` = `assignments`.`course2teacherId` 
       AND `course2teacher`.`id` = `follows`.`course2teacherid` 

这是我收到的错误消息:#2014 - Commands out of sync; you can't run this command now

为什么会发生这种情况,有没有办法解决它?

1 个答案:

答案 0 :(得分:0)

ž

在启动另一个查询之前打开结果集然后不关闭时,通常会发生此错误。

我认为这会重新创建您的错误(出于说明目的的最小示例):

$CMySQL->query($VSQL);

$RResult = $CMySQL->store_result();

$ARow = $RResult->fetch_assoc();

$CMySQL->query($VSQL); // error occurs here as result set is still open

要解决此问题,可以使用以下代码:

$CMySQL->query($VSQL);

$RResult = $CMySQL->store_result();

$ARow = $RResult->fetch_assoc();

$RResult->free();

$CMySQL->next_result()

$CMySQL->query($VSQL); // should work now

总结一下,我不认为您的错误的原因在于查询,而是在代码中较早发生的事情导致您的MySQL连接被卡住。要确定麦伟锋所提到的这一点,您可以尝试在独立客户端中运行查询,或者如果您想在代码中执行此操作,请为此查询打开一个新连接,以查看是否再次出现错误。

此致

詹姆斯