开发者

Getting row data of inserted row (Mysql, PHP, mysqli)

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();
      开发者_StackOverflow社区  $json['postid']  = $row['postid'];
        $result->free();

where $title is the title name of the newly-inserted post.

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?

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


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.


insert_id http://php.net/manual/en/mysqli.insert-id.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜