Php 依靠;mysql“插入”id“;
设想一个名为Php 依靠;mysql“插入”id“;,php,mysql,Php,Mysql,设想一个名为foo的表,它有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_l
foo
的表,它有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);
}
我在insert查询后立即调用它,并在获取最后一个插入id
时使用当前的连接链接,但不知道这有什么关系(?)
我是否应该担心此实现依赖于
mysql\u insert\u id
?最后一个\u insert\u 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);
}
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);
}
有关更多信息,请参见:
“对于最后一个插入的ID(),最近生成的ID在
基于每个连接的服务器。它不会被其他连接更改
客户端。如果更新另一个自动增量,它甚至不会更改
具有非magic值的列(即,不为NULL的值和
不是0)。使用最后的插入ID()和自动增量列
同时从多个客户端发送的消息是完全有效的
将接收该客户端最后一条语句的最后一个插入ID
执行。”
所以你应该做你想做的事情,而且你不应该得到奇怪的结果。查看更多信息,上面说:
“对于最后一个插入的ID(),最近生成的ID在
基于每个连接的服务器。它不会被其他连接更改
客户端。如果更新另一个自动增量,它甚至不会更改
具有非magic值的列(即,不为NULL的值和
不是0)。使用最后的插入ID()和自动增量列
同时从多个客户端发送的消息是完全有效的
将接收该客户端最后一条语句的最后一个插入ID
执行。”
所以你应该做你想做的事情,而且你不应该得到奇怪的结果。@Anton是正确的。希望添加以下内容 在事务中使用时,必须先调用mysql\u insert\u id() 承诺。否则,它将返回不可预测的结果
礼貌是正确的。希望添加以下内容 在事务中使用时,必须先调用mysql\u insert\u id() 承诺。否则,它将返回不可预测的结果
礼貌谢谢@Hemant;但我正在将
$this->last\u insert\u id
立即存储在$db->query()
函数“if$query
包含insert
关键字”中。尝试跳过querytanks@Hemant中的id字段;但是我正在将$this->last\u insert\u id
立即存储在$db->query()
函数“if$query
包含insert
关键字”中。请尝试在查询中使用id字段跳过