MySQL:LAST_INSERT_ID() 返回 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13880267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: LAST_INSERT_ID() returns 0
提问by dwelle
I've got this test table:
我有这个测试表:
CREATE TABLE IF NOT EXISTS `test` (
`id` INT(10) AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4;
inserting using either of these three
使用这三个中的任何一个插入
INSERT INTO `test` (`id`) VALUES (NULL);
INSERT INTO `test` (`id`) VALUES (0);
INSERT INTO `test` () VALUES ();
and issuing
并发行
SELECT LAST_INSERT_ID();
but the query always results in 0
.
但查询总是导致0
.
PHP's mysql_insert_id
and PDO::lastInsertId()
yield no result either.
PHP'smysql_insert_id
并且也PDO::lastInsertId()
不会产生任何结果。
I've been toying with this whole day and can't get it to work. Ideas?
我一整天都在玩弄这件事,但无法让它发挥作用。想法?
回答by dwelle
The problem seemed to be in MySQL's phpmyadmin config file PersistentConnections
set to FALSE
which resulted in a new CONNECTION_ID
every time a query was issued - therefore rendering SELECT LAST_INSERT_ID()
ineffective.
问题似乎出在 MySQL 的 phpmyadmin 配置文件PersistentConnections
中FALSE
,CONNECTION_ID
每次发出查询时都会产生一个新的配置文件- 因此呈现SELECT LAST_INSERT_ID()
无效。
more info in the subsequent topic Every query creates a new CONNECTION_ID()
后续主题中的更多信息每个查询都会创建一个新的 CONNECTION_ID()
Also thanks dnagirlfor help
也感谢dnagirl的帮助
回答by Ted
Just my 50 cents for this issue, I simply noticed that you won't get a LAST_INSERT_ID
greater than 0
if your table has no AUTO_INCREMENT
set to an index.
对于这个问题,我只花了 50 美分,我只是注意到,如果您的表没有设置索引,您将不会获得LAST_INSERT_ID
更大的收益。0
AUTO_INCREMENT
I wasted about half hour on this. Turns out I keep getting a LAST_INSERT_ID()
of 0
, which for this table is actually ok.
我在这上面浪费了大约半个小时。结果我一直得到 a LAST_INSERT_ID()
of 0
,这对于这张桌子来说实际上是可以的。
回答by user7282
you have to combine
你必须结合
INSERT INTO test (title) VALUES ('test');SELECT LAST_INSERT_ID();
Then you will get the last insert id
然后你会得到最后一个插入 id
回答by micaball
I had the same issue. mysql_insert_id()
or LAST_INSERT_ID()
returned 0 when requested inside a PHP function with an INSERT query.
我遇到过同样的问题。mysql_insert_id()
或LAST_INSERT_ID()
在 PHP 函数中使用INSERT 查询请求时返回 0 。
I sorted the issue by requesting the value of mysql_insert_id()
from a separate PHP function called right after the function that INSERT query.
我通过mysql_insert_id()
从INSERT 查询函数之后调用的单独 PHP 函数请求 的值来对问题进行排序。
回答by Gautier Logeot
I Had the same issues but I have resolved this by creating a transaction.
我遇到了同样的问题,但我已经通过创建事务解决了这个问题。
$db->begin();
$sql = "INSERT INTO 'test' VALUES('test')";
if ($db->query($sql))
{
$id = $db->last_insert_id('test');
$db->commit();
return $id;
}
else{
$db->rollback();
return -1;
}
I had tried
我试过了
return $db->last_insert_id('test');
after the "commit" but that always returned "0"
在“提交”之后但总是返回“0”
hope that can help you
希望能帮到你
回答by Premkumar S
it work perfectly...try it...
它完美地工作......试试吧......
$result = mysql_query("INSERT INTO `test` (`title`) VALUES ('test')");
if ($result) {
$id = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM tablename WHERE id = $id") or die(mysql_error());
// return user details
if (mysql_num_rows($result) > 0) {
return mysql_fetch_array($result);
} else {
return false;
}
} else {
return false;
}