问题描述
插入时,我需要找回行的postid(自动递增PK).
I need to get back the postid (auto-incrementing PK) of a row when i insert it.
我目前正在使用它来获取它
I am currently using this to get it
//get postid to return
if($result = $db -> query('SELECT postid FROM posts WHERE title = \''.$title.'\' LIMIT 1')){
$row = $result->fetch_assoc();
$json['postid'] = $row['postid'];
$result->free();
其中$ title是新插入的帖子的标题名称.
where $title is the title name of the newly-inserted post.
mysqli类的一部分是否可以让我在一个查询中执行此操作? $ db-> query()是否返回任何信息,以使此操作更简单,更安全?
Is there part of the mysqli class that will allow me to do this in one query?? Does $db -> query() return any information that will make this simpler and more secure?
我尝试浏览mysqli文档,但是找不到我想要的东西.我确定它在某处.
I have tried looking through the mysqli documentation, but I could not find what i wanted. I'm sure it is there somewhere.
多个标题会搞砸这一点,尽管不允许使用,但您不能太安全
Multiple titles will screw this up, and although they are not allowed, you cant be too safe
推荐答案
您的表中是否有AUTO_INCREMENT列?如果是这样,您可以使用 mysql_insert_id()
或 mysqli->insert_id
获取该值,然后使用它选择该行. 此处有更多信息.
Does your table have an AUTO_INCREMENT column? If so you can use mysql_insert_id()
or mysqli->insert_id
to get that value, and then use it to select the row. More info here.
这篇关于获取插入行的行数据(Mysql,PHP,mysqli)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!