Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 是否在最后一个查询之前获取查询的最后一个插入ID?_Mysql_Pdo - Fatal编程技术网

Mysql 是否在最后一个查询之前获取查询的最后一个插入ID?

Mysql 是否在最后一个查询之前获取查询的最后一个插入ID?,mysql,pdo,Mysql,Pdo,如何获取主题id?所以我可以在用户发布帖子后将其重定向到他的主题? 如果我将其切换,则无法在帖子中插入主题id。嗯 $stmt = $db->prepare("INSERT INTO topics (subject, posted, first_post_id, last_post, last_post_id, forum_id) VALUES (:subject,:posted,:first_post_id,:last_post,:last_post_id,:forum_id)");

如何获取主题id?所以我可以在用户发布帖子后将其重定向到他的主题? 如果我将其切换,则无法在帖子中插入主题id。嗯

$stmt = $db->prepare("INSERT INTO topics (subject, posted, first_post_id, last_post, last_post_id, forum_id) 
VALUES (:subject,:posted,:first_post_id,:last_post,:last_post_id,:forum_id)");
$stmt->execute(array(':subject'=>$subject,':posted'=>time(),':first_post_id'=>$cid,':last_post'=>time(),':last_post_id'=>$cid,':forum_id'=>$f));


$stmt = $db->prepare("INSERT INTO posts (poster_id, poster_ip, message, posted, topic_id) 
 VALUES (:poster_id,:poster_ip,:message,:posted,:topic_id)");
$stmt->execute(array(':poster_id'=>$cid,':poster_ip'=>$_SERVER['REMOTE_ADDR'],':message'=>$message,':posted'=>time(),':topic_id'=>$db->lastInsertId()));




header("Location: /thread/".$db->lastInsertId()); // This will get the post ID not topic ID that i want it to

使用临时变量:

$stmt = $db->prepare(/* FIRST QUERY */);
$stmt->execute();

$temp = $db->lastInsertId();

$stmt = $db->prepare(/* SECOND QUERY */);
$stmt->execute();

header("Location: /thread/".$temp);

哦当然哇,我怎么没想到我现在觉得自己很傻哈哈