通过PHP使用LAST_INSERT_ID()? [英] Using LAST_INSERT_ID() via PHP?
问题描述
当我在MySQL控制台中执行以下命令时,它工作正常:
When I execute the following in the MySQL console, it works fine:
INSERT INTO videos (embed_code) VALUES ('test code here');
SELECT LAST_INSERT_ID();
但是,当我通过PHP执行上述查询时,结果为空.
But, when I execute the above queries via PHP, the result is empty.
在数据抽象下,我正在使用名为DB
的数据库访问类.这就是我通过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:
$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];
通过embed_code选择新的ID对我来说并不可靠,因为embed_code可以重复.
It is not reliable for me to select the new id by the embed_code, as the embed_code could be repeated.
谢谢!
推荐答案
您的数据库类是否不包含用于获取最后一个插入ID的函数?否则,请尝试mysql_insert_id().
Doesn't your database class contain a function to grab the last insert ID? Try mysql_insert_id() otherwise instead.
http://nl.php.net/mysql_insert_id
这篇关于通过PHP使用LAST_INSERT_ID()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!