【问题标题】:Get the primary key of an Inserted value获取插入值的主键
【发布时间】:2013-08-17 20:31:38
【问题描述】:

在 Android 上,当我向我的数据库中插入一行时,我可以在插入后轻松获取其 ID:

long id=database.insert(MySqlHelper.TABLE_UPLOAD, null,values);

是否有类似的PHP 函数来检索插入行的Primary key,而不使用另一个Select 语句,就像Android

【问题讨论】:

  • 如果你用的是mysql,函数是mysql_insert_id()
  • 你为什么不把它作为答案发布?

标签: php database insert-statement


【解决方案1】:

是的,有:mysql_insert_id,见http://php.net/manual/en/function.mysql-insert-id.php

如果您使用 MySQLi:mysqli_insert_id,请参阅http://www.php.net/manual/en/mysqli.insert-id.php

如果您使用 PDO:PDO::lastInsertId,请参阅 http://www.php.net/manual/en/pdo.lastinsertid.php

【讨论】:

  • 这不适用于像 uuids 这样的非数字主键。
猜你喜欢
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
相关资源
最近更新 更多