使用 PHP 获取最后插入的 auto_incrementID MySQL
Get last inserted auto_incrementID MySQL using PHP
我尝试在执行 sqlstatement 后获取最后插入的 auto_incremented ID。我正在使用 slim 框架来做到这一点。这是我的代码:
$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增加的值:
return mysqli_insert_id($this->conn);
return $this->conn->mysqli_insert_id();
return $this->conn->insert_id;
但这行不通...
有人知道如何获取值吗?
感谢帮助!
您正在使用 INSERT DELAYED
,因此在您调用该代码时它可能还没有插入。
When a client uses INSERT DELAYED, it gets an okay from the server at
once, and the row is queued to be inserted when the table is not in
use by any other thread.
更具体地说
Because the INSERT DELAYED statement returns immediately, before the
rows are inserted, you cannot use LAST_INSERT_ID() to get the
AUTO_INCREMENT value that the statement might generate. from
http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html
所以如果你想获取ID,请不要使用延迟。
我尝试在执行 sqlstatement 后获取最后插入的 auto_incremented ID。我正在使用 slim 框架来做到这一点。这是我的代码:
$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增加的值:
return mysqli_insert_id($this->conn);
return $this->conn->mysqli_insert_id();
return $this->conn->insert_id;
但这行不通...
有人知道如何获取值吗?
感谢帮助!
您正在使用 INSERT DELAYED
,因此在您调用该代码时它可能还没有插入。
When a client uses INSERT DELAYED, it gets an okay from the server at once, and the row is queued to be inserted when the table is not in use by any other thread.
更具体地说
Because the INSERT DELAYED statement returns immediately, before the rows are inserted, you cannot use LAST_INSERT_ID() to get the AUTO_INCREMENT value that the statement might generate. from
http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html
所以如果你想获取ID,请不要使用延迟。