繁体   English   中英

获取最后插入值的ID

[英]Getting an ID for the last inserted value

我正在尝试获取数据库最近添加的值的唯一ID。 我尝试使用LastInsertID,但我认为这与MySQL(http://www.php.net/manual/zh/pdo.lastinsertid.php)不兼容。

$sql = "INSERT INTO discussion_links (link_url, link_title, link_source, publish_date, link_side, img_link) VALUES (?, ?, ?, ?, ?, ?)";
$sth=$db->prepare($sql);
$sth->execute(array($_POST['OP_link_url'], $_POST['OP_title'], $_POST['OP_source'], $_POST['OP_pub_date'], $_POST['OP_disc_side'], $_POST['OP_img_url']));
$op_link_id = $sth->$db->lastInsertID();

在这里,我得到了错误:PHP可捕获的致命错误:PDO类的对象无法转换为字符串

我还尝试将最后一行作为:

$op_link_id = $sth->fetch(PDO::lastInsertID);

$temp = $sth->fetch(PDO::FETCH_ASSOC);
$temp_op_link_id = $temp['link_id'];

但是,没有一个有效(得到了一些SQLState General Error 2053)。

谢谢,

应该在PDO实例上调用lastInsertID()

$op_link_id = $sth->$db->lastInsertID();

应该

$op_link_id = $db->lastInsertID();

尝试这个

$op_link_id = $db->lastInsertID();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM