php - 依赖 "mysql_insert_id"

标签 php mysql

考虑一个名为 foo 的表,它有 id (PRIMARY & AUTO_INCREMENT) 列。我正在向该表中插入一行,挑战从此时开始。

$db->query("INSERT INTO `foo` (id, row1, ...) VALUES('', '$row1', ...)");
if (!$db->is_error && $db->last_insert_id) {
   $id = $db->last_insert_id;
   do_really_important_things_with_last_ID($id);
   do_another_important_things_with_last_ID($id);
   ...
   do_ ... with_last_ID($id);
   do_ ... with_last_ID($id);
}

我在插入查询后立即调用它并在获取 last_insert_id 时使用准确的当前连接链接,但不知道这有什么关系(?)。

$this->dbh = @mysql_connect(.....);
...
// query function
$this->result = @mysql_query($query, $this->dbh);
...
if (preg_match("/insert/i", $query)) {
   $this->last_insert_id = @mysql_insert_id($this->dbh);
}


我是否应该担心此实现依赖于 mysql_insert_id

最佳答案

last_insert_id 应该是一个函数,它将返回插入的 id

function last_insert_id(){
 return @mysql_insert_id();
}

if (!$db->is_error && $db->last_insert_id()) {
   $id = $db->last_insert_id();
   do_really_important_things_with_last_ID($id);
   do_another_important_things_with_last_ID($id);
   ...
   do_ ... with_last_ID($id);
   do_ ... with_last_ID($id);
}

关于php - 依赖 "mysql_insert_id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8634903/

相关文章:

javascript - 如何将 favicon 链接到随机 css 颜色?

AWS 上挂载卷的 Mysql k8s Pod 失败

MySQL根据先前的查询结果交织两个查询

php - 使用 Memcache 和 Doctrine 1.2 进行多域缓存

Javascript 追加和撤消问题

mysql - 从 MYSQL 将数据输入到 Google 柱形图中的 2 个以上的列。

MySQL 多选查询

sql - MySQL 'IN'子句及返回的记录集顺序

围绕第 3 方代码改造命名空间的 PHP 策略

php mysql全文搜索通过id连接的多个表