Getting row data in an inserted row (Mysql, PHP, mysqli)

I need to return postid (auto-incrementing PK) strings 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(); 

where $ title is the name of the name of the newly inserted record.

Is there a part of the mysqli class that will allow me to do this in a single query? $ Db → query () returns any information that will make it simpler and safer?

I tried looking at the mysqli documentation, but I could not find what I wanted. I'm sure he's out there somewhere.

Several headers will ruin this, and although they are not allowed, you cannot be too secure.

+4
source share
2 answers

Does your table have an AUTO_INCREMENT column? If so, you can use mysql_insert_id() or mysqli->insert_id to get this value, and then use it to select a row. More info here .

+11
source

All Articles