通过 PHP 使用 LAST_INSERT_ID()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3425625/
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
Using LAST_INSERT_ID() via PHP?
提问by Mike Moore
When I execute the following in the MySQL console, it works fine:
当我在 MySQL 控制台中执行以下命令时,它工作正常:
INSERT INTO videos (embed_code) VALUES ('test code here');
SELECT LAST_INSERT_ID();
But, when I execute the above queries via PHP, the result is empty.
但是,当我通过 PHP 执行上述查询时,结果为空。
Under the data abstraction, I am using a database access class named DB
. This is how I have tried the above queries via PHP:
在数据抽象下,我使用了一个名为DB
. 这就是我通过 PHP 尝试上述查询的方式:
$embedCode = htmlentities($_POST['embed_code']);
//Insert video in database **WORKS**
DB::query("INSERT INTO videos (embed_code) VALUES ('$embedCode');");
//Retrieve id **DOES NOT WORK**
$row = DB::getSingleRow("SELECT LAST_INSERT_ID();");
$videoId = $row[0];
It is not reliable for me to select the new id by the embed_code, as the embed_code could be repeated.
通过 embed_code 选择新 id 对我来说是不可靠的,因为 embed_code 可能会重复。
Thanks!
谢谢!
采纳答案by Sebs
Doesn't your database class contain a function to grab the last insert ID? Try mysql_insert_id() otherwise instead.
您的数据库类不包含获取最后一个插入 ID 的函数吗?否则请尝试 mysql_insert_id() 。
回答by David
PHP has a function called mysql_insert_id
which will give you the same result as the query SELECT LAST_INSERT_ID();
.
PHP 有一个被调用的函数mysql_insert_id
,它会给你与查询相同的结果SELECT LAST_INSERT_ID();
。
回答by John Parker
There are dedicated functions for this - you shouldn't be using the SQL version as it doesn't work.
有专门的功能 - 您不应该使用 SQL 版本,因为它不起作用。
In the MySQL library you should use the mysql_insert_idfunction, whereas the mysqli object has an insert-idproperty. This of course may already be exposed in whatever DB class you're using.
在 MySQL 库中,您应该使用mysql_insert_id函数,而 mysqli 对象有一个insert-id属性。这当然可能已经在您使用的任何 DB 类中公开。
回答by Pablo Santa Cruz
You can try with PHP's mysql_insert_id
function.
您可以尝试使用 PHP 的mysql_insert_id
功能。
I think what's happening is that each DB::query
call is in a different transaction, hence, $row
is being empty. Maybe you can read DB:query
documentation to try running both calls within the same transaction.
我认为正在发生的事情是每个DB::query
调用都在不同的事务中,因此$row
是空的。也许您可以阅读DB:query
文档以尝试在同一事务中运行两个调用。
回答by user596387
mysql_insert_id is deprecated. So I think this thread needs to be updated.
不推荐使用 mysql_insert_id。所以我认为这个线程需要更新。
回答by Franz Holzinger
mysql_insert_id is marked as deprecated since PHP 5.5.0.
自 PHP 5.5.0 起,mysql_insert_id 被标记为已弃用。
In this example uid is the primary key of the table videos.
在这个例子中,uid 是表视频的主键。
$row = DB::getMultipleRows("SELECT uid FROM videos WHERE uid=LAST_INSERT_ID();");
if (
isset($row) &&
is_array($row) &&
isset($row['0']) &&
is_array($row['0']) &&
isset($row['0']['uid'])
) {
$videoId = $row['0']['uid'];
}
回答by saeed
you should return lastInsertId of PDO to outer of your class . example:
您应该将 PDO 的 lastInsertId 返回到您的类的外部。例子:
class DB
{
public $last_insert_id;
function x()
{
$stmt = $db->prepare($Query);
$stmt->execute();
$this->last_insert_id = $db->lastInsertId();
}
}