SQL语法还可以,但是php仍然会抛出错误

时间:2011-05-07 09:45:05

标签: php mysql sql mysql-error-1064

当我在phpmyadmin中手动检查时我的sql查询工作正常,但是当我尝试通过php mysql_query处理它时抛出一个语法错误。如何解决这个问题?

错误讯息:

Invalid query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO scores( user_id ) VALUES (LAST_INSERT_ID( ))' at line 1 
Whole query:
INSERT INTO users (id_fb, name, first_name, last_name, email, link, first_login) 
VALUES ('1000001010101010', 'Bart Roza', 'Bart', 'Roza', 'lalalala@gmail.com','http://www.facebook.com/profile.php?id=1000001010101010','2011-05-07 11:15:24'); 
INSERT INTO scores( user_id ) VALUES (LAST_INSERT_ID( ));

我的php功能:

public function createUser()
{
    $time = date("Y-m-d H:i:s");

    $insert = "INSERT INTO users (id_fb, name, first_name, last_name, email, link, first_login) VALUES (" .
              "'" . $this->me['id'] . "', " .
              "'" . $this->me['name'] . "', " .
              "'" . $this->me['first_name'] . "', " .
              "'" . $this->me['last_name'] . "', " .
              "'" . $this->me['email'] . "'," .
              "'" . $this->me['link'] . "'," .
              "'" . $time . "'); " .
              "INSERT INTO scores( user_id ) VALUES (LAST_INSERT_ID( ));";

    $result = mysql_query($insert);
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $insert;
        die($message);
    }
}

编辑:

感谢您的解决方案!

3 个答案:

答案 0 :(得分:5)

由于mysql_query只接受一个查询,因此您需要将查询字符串拆分为2个独立的查询,并通过2次mysql_query调用执行此操作。

答案 1 :(得分:2)

使用mysql_query功能无法一次运行多个查询。您必须使用单独的mysql_query调用

运行这两个查询
  

mysql_query()发送一个唯一的查询   (不支持多个查询)

答案 2 :(得分:0)

AS @zerkms和@Shakti说,mysql_query不支持多个查询。如果要使用此类功能,请考虑迁移到mysqli。它通过mysqli_multi_query

支持单个数据包中的多个查询