[英]Getting an ID for the last inserted value
I am trying to get the unique ID for the most recently added value to the database. 我正在尝试获取数据库最近添加的值的唯一ID。 I tried using LastInsertID bu I believe this is not compatible with MySQL (http://www.php.net/manual/en/pdo.lastinsertid.php).
我尝试使用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();
Here I get the error: PHP Catchable fatal error: Object of class PDO could not be converted to string 在这里,我得到了错误:PHP可捕获的致命错误:PDO类的对象无法转换为字符串
I also tried doing that last line as: 我还尝试将最后一行作为:
$op_link_id = $sth->fetch(PDO::lastInsertID);
And 和
$temp = $sth->fetch(PDO::FETCH_ASSOC);
$temp_op_link_id = $temp['link_id'];
But neither one worked (got some SQLState General Error 2053). 但是,没有一个有效(得到了一些SQLState General Error 2053)。
Thanks, 谢谢,
lastInsertID()
should be called on PDO instance. 应该在PDO实例上调用
lastInsertID()
。
$op_link_id = $sth->$db->lastInsertID();
Should be 应该
$op_link_id = $db->lastInsertID();
尝试这个
$op_link_id = $db->lastInsertID();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.