{"id":377,"date":"2018-01-15T02:34:25","date_gmt":"2018-01-15T02:34:25","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=377"},"modified":"2018-01-15T02:34:25","modified_gmt":"2018-01-15T02:34:25","slug":"get-id-of-the-last-inserted-record","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/get-id-of-the-last-inserted-record\/","title":{"rendered":"Get ID of The Last Inserted Record"},"content":{"rendered":"<p>You can simply get the last insert record id by using php insert_id statement.<\/p>\n<p><strong>Example Code:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$servername = &quot;localhost&quot;;<br \/>\n$username = &quot;username&quot;;<br \/>\n$password = &quot;password&quot;;<br \/>\n$dbname = &quot;myDB&quot;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} <\/p>\n<p>$sql = &quot;INSERT INTO MyGuests (firstname, lastname, email)<br \/>\nVALUES (&#8216;John&#8217;, &#8216;Doe&#8217;, &#8216;john@example.com&#8217;)&quot;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\n    $last_id = $conn-&gt;insert_id;<br \/>\n    echo &quot;New record created successfully. Last inserted ID is: &quot; . $last_id;<br \/>\n} else {<br \/>\n    echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . $conn-&gt;error;<br \/>\n}<\/p>\n<p>$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code, we have just added one additional statement to get last inserted id.<\/p>\n<p><code>if ($conn->query($sql) === TRUE) {<br \/>\n    $last_id = $conn->insert_id;<br \/>\n    echo \"New record created successfully. Last inserted ID is: \" . $last_id;<br \/>\n}<\/code><\/p>\n<p>In the if statement, once we confirm the connection, is true, on the next line we are asking php to check the last <code>insert_id<\/code>. The php script will check the statement and return the value in the echo statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can simply get the last insert record id by using php insert_id statement. Example Code: [php]&lt;?php $servername = &quot;localhost&quot;; $username = &quot;username&quot;; $password =<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/377\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}