【问题标题】:Get the last inserted system id of a certain table before doing any query pdo php在执行任何查询 pdo php 之前获取某个表的最后插入的系统 ID
【发布时间】:2015-03-20 08:34:06
【问题描述】:

this 教程中,我找到了一种获取特定表的最后插入 id 的方法(使用 pdo)。在示例中

$result = $db->exec("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");
$insertId = $db->lastInsertId();

他在获取 id 之前进行了查询,我想做这样的事情,但我想在进行任何查询之前先获取他最后插入的条目的 id。How should i tell the code that it should check the last inserted id from this table。任何想法都值得赞赏。

Update

我想获取特定表last inserted id,而不仅仅是数据库最后插入的id

【问题讨论】:

标签: php mysql pdo


【解决方案1】:

试试这个...

它在任何查询运行之前返回最后插入的 id。

select * from yourtable order by id desc limit 0,1

【讨论】:

  • 在查询运行lastinsertedid() 后如何获取 id ?
  • $stmt = $db->query("SELECT LAST_INSERT_ID()"); $lastId = $stmt->fetch(PDO::FETCH_NUM); $lastId = $lastId[0];
  • 我可以在 lastinsertedid 之后添加一个 where 子句,例如 where location = asia 类似这样的东西吗?我也想为此选择一个特定的表
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 2012-05-27
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多