mysql 5.7

时间:2016-08-02 09:24:03

标签: php mysql

我试图运行一个简单的UPDATE查询:

UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."'; UPDATE timeLimit  SET listed = listed + 1  WHERE enterTime = '".$enterTime."' AND building = '".$building."';"

它在MySql版本5.1中工作正常,我的服务器已升级到MySql版本5.7,我收到以下错误:

  

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在'UPDATE学生项目附近使用正确的语法SET enterTime = '09:00'WHERE'在第1行   开始; UPDATE学生SET enterTime = '09:00'WHERE s_id ='312433931'; UPDATE timeLimit SET列出=列出+ 1 WHERE enterTime = '09:00'AND building ='2'; UPDATE timeLimit SET列出=已列出 - 1 WHERE enterTime = '08:00'AND building ='2'; COMMIT;

PHP代码:

function update_time_for_student($student,$enterTime,$oldTime){
    $s_id = $student['s_id'];
    $building = $student['building'];
    $query = "BEGIN; UPDATE students 
                SET enterTime = '".$enterTime."' 
                WHERE s_id = '".$s_id."' ;              
               UPDATE timeLimit 
                SET listed = listed + 1 
                WHERE enterTime = '".$enterTime."' AND building = '".$building."';
              UPDATE timeLimit 
                SET listed = listed - 1 
                WHERE enterTime = '".$oldTime."' AND building = '".$building."'; COMMIT;";
    if (mysql_query($query)){
        $fullName = $student['fname']." ".$student['lname'];
        send_email($student['email'],$enterTime,$fullName,$s_id);
        header("Location:/index.php?d=1"); 
    } else {
        echo mysql_error();
        echo "<br/>";
        echo $query;
        //header("Location:/index.php?d=3");
    }  
}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

选项-1

尝试将这些行分成多个php语句:

$query = "BEGIN";
mysql_query($query) or die (mysql_error());

$query = "UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."';";
mysql_query($query) or die (mysql_error());

$query = "UPDATE timeLimit  SET listed = listed + 1  WHERE enterTime = '".$enterTime."' AND building = '".$building."';";
mysql_query($query) or die (mysql_error());

$query = "COMMIT";
mysql_query($query) or die (mysql_error());

选项-2

您需要使用multi_query代替。文档here

注意: mysql_ *命令在PhP7中已弃用并删除。而是使用mysqli或PDO。请参阅接受的答案here