请考虑一个名为foo
的表,其中有id (PRIMARY & AUTO_INCREMENT)
列。我在这张桌上插入一行,挑战从这一点开始。依赖于“mysql_insert_id”
$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
?
Thanks @Hemant;但是,如果$ query包含'INSERT'关键字“,那么我会立即在$ db-> query()'function”中存储'$ this-> last_insert_id'。 – 2011-12-26 10:53:08
通过在查询中跳过使用id字段来尝试 – Dau 2011-12-26 10:55:00