如何在创建主ID之前或同时获取主ID? [英] How to get a Primary ID before or at the same time it gets created?

查看:82
本文介绍了如何在创建主ID之前或同时获取主ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么:(wp_posts具有ID作为主键,并且会自动增加)

mysql_query("INSERT INTO wp_posts (post_content, post_title) VALUES ('$addEP', '$title')");
$rs = mysql_query("SELECT ID FROM wp_posts WHERE post_title='$title'");
$add_row = mysql_fetch_array($rs);
$add_postid = $add_row['ID'];
mysql_query("INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES ('$add_postid', '$cat_id')");

所以基本上,我在做什么?我将内容插入wp_posts,然后运行另一个查询以捕获帖子的ID.然后将其设置到另一个表中.

是否可以在不运行第二个查询来捕获ID的情况下执行此操作? 谢谢.

解决方案

$add_postid = mysql_insert_id();是您要寻找的.

所以

$rs = ... // and
$add_row = ...

...不再是必需的.

What I am doing: (wp_posts has ID as primary key and it auto-increase)

mysql_query("INSERT INTO wp_posts (post_content, post_title) VALUES ('$addEP', '$title')");
$rs = mysql_query("SELECT ID FROM wp_posts WHERE post_title='$title'");
$add_row = mysql_fetch_array($rs);
$add_postid = $add_row['ID'];
mysql_query("INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES ('$add_postid', '$cat_id')");

So basically, what I am doing is; I insert content into wp_posts and then run another query to catch the ID of the post. Then set it into another table.

Is it possible to do this without running the second query to catch the ID? Thanks.

解决方案

$add_postid = mysql_insert_id(); is what you are looking for.

So

$rs = ... // and
$add_row = ...

... is not necessary anymore.

这篇关于如何在创建主ID之前或同时获取主ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆