通过PHP使用LAST_INSERT_ID()?

Mik*_*ore 7 php mysql lastinsertid

当我在MySQL控制台中执行以下操作时,它可以正常工作:

INSERT INTO videos (embed_code) VALUES ('test code here');
SELECT LAST_INSERT_ID();
Run Code Online (Sandbox Code Playgroud)

但是,当我通过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];
Run Code Online (Sandbox Code Playgroud)

我不能通过embed_code选择新的id,因为embed_code可以重复.

谢谢!

小智 5

您的数据库类是否包含获取最后一个插入ID的函数?相反,请尝试使用mysql_insert_id().

http://nl.php.net/mysql_insert_id