0

我正在尝试获取数据库中最近添加的值的唯一 ID。我尝试使用 LastInsertID bu 我相信这与 MySQL (http://www.php.net/manual/en/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 Catchable fatal error: Object of class PDO could not be convert to string

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

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

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

但没有一个工作(得到一些 SQLState 一般错误 2053)。

谢谢,

4

2 回答 2

4

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

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

应该

$op_link_id = $db->lastInsertID();
于 2012-07-03T05:48:53.200 回答
2

试试这个

$op_link_id = $db->lastInsertID();
于 2012-07-03T05:49:52.747 回答