php - MySQL- 为什么 LAST_INSERT_ID() 对我不起作用?

标签 php mysql

我有以下代码:

  public function createNewGuide($userID,$guideName)
  {
    $sql =" INSERT INTO myTable(name, updated) 
            VALUES ('$guideName', 'NOW()')";

    //Process query
    $this->query($sql); // This inserts the new row
    $this->query('LAST_INSERT_ID()'); // This throws an error

    return $this->query_result;
  }

我的查询函数如下所示:

  private function query($sql) 
  {
      $this->query_result = mysql_query($sql, $this->conn)
        or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");   
  } 

我收到以下错误:

MySQL Database Error: 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 'LAST_INSERT_ID()'

我用谷歌搜索并查看了类似的问题,但没有找到答案:(

我还没有尝试过 PHP function mysql_insert_id() ,因为我真的很想用 SQL 来做到这一点。

最佳答案

为什么不直接使用 PHP 的 mysql_insert_id

不考虑...

SELECT LAST_INSERT_ID()

...只要您在表中有一个自动递增列就应该可以工作

关于php - MySQL- 为什么 LAST_INSERT_ID() 对我不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2033366/

相关文章:

php - 设置.htaccess规则以指定不同的文件夹

php - 屏幕抓取时我应该使用 Keep-Alive 吗?

php - MySQL - 在 $var = array 的位置获取数据?

mysql - 使用嵌套选择 MySQL 语句的错误

java - 如何使用jsp更新mysql表?

mysql - Yii2,在连接中合并两个事件查询条件

php - Laravel - 仅具有外键的表模型(数据透视表)

php - Blueimp jQuery 文件上传插件 - "Empty file upload"结果 PHP

php - 多次调用 Eloquent 动态属性会多次访问数据库吗?

php - php将数组插入mysql数据库