【问题标题】:Get last inserted auto_incrementID MySQL using PHP使用 PHP 获取最后插入的 auto_incrementID MySQL
【发布时间】:2015-05-21 22:41:49
【问题描述】:

我尝试在执行 sqlstatement 后获取最后插入的 auto_incremented ID。我正在使用苗条的框架来做到这一点。这是我的代码:

$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

...

$sql_stmt= "INSERT DELAYED INTO Tab(Id, Value1, Value2, Value3) VALUES (DEFAULT,?,?,?)";

$stmt= $this->conn->prepare($sql_stmt);
$stmt->bind_param("sss", $param1, $param2, $param3);
$stmt->execute();

这是我尝试返回的增量值:

return mysqli_insert_id($this->conn);
return $this->conn->mysqli_insert_id();
return $this->conn->insert_id;

但这不起作用...

有人知道如何获取价值吗?

感谢您的帮助!

【问题讨论】:

    标签: php mysql insert auto-increment


    【解决方案1】:

    您使用的是INSERT DELAYED,所以在您调用该代码时它可能甚至没有被插入。

    当客户端使用 INSERT DELAYED 时,它会在 一次,当表不在时,该行排队等待插入 由任何其他线程使用。

    更具体的

    因为 INSERT DELAYED 语句立即返回,在 行被插入,你不能使用 LAST_INSERT_ID() 来获取 语句可能生成的 AUTO_INCREMENT 值。来自

    http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html

    所以如果要获取ID,请不要使用延迟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      相关资源
      最近更新 更多