当我在MySQL数据库中创建一个新条目时,我希望查询返回刚刚创建的表的id。如果它不能做到这一点,有没有其他方法可以找到this id?我想这样做的原因是,我可以将新行链接到生成它的行(并且不能保证它们是连续的)。
发布于 2009-03-21 20:34:48
你是说最后一个自动增量id?
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>发布于 2009-03-21 20:35:39
发布于 2009-03-21 20:35:46
您是说刚刚插入到表中的记录的ID吗?如果是这样,您正在查找mysql_insert_id。
https://stackoverflow.com/questions/669942
复制相似问题