php - SQL 语法没问题,但 php 仍然抛出错误

标签 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);
    }
}

编辑:

感谢您的解决方案!

最佳答案

由于 mysql_query 只接受一个查询,您需要将查询字符串拆分为 2 个独立的查询,并使用 2 个 mysql_query 调用来执行。

关于php - SQL 语法没问题,但 php 仍然抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5920285/

相关文章:

javascript - 如何将 ajax post 连接到 PHP 文件中的特定条件?

SQL 服务器 : how to delete specific rows data with where condition in column?

mysql - 在sql中使用剩余值减去

php - Json 编码对某些字符返回空值

php - ajax 不响应所有数组数据

php - 我想在插入或更新 mysql 之前检查

php - php代码中的错误mysql更新命令

mysql - 如何将数据插入到两个表中,且第一个表的 PK 未知?

PHP PDO 上一个函数的lastinsertid

php - 在另一个表中发生不相关的插入操作后,Mysql 行被删除