{"id":12222,"date":"2023-01-16T05:05:35","date_gmt":"2023-01-16T05:05:35","guid":{"rendered":"https:\/\/www.webhozz.com\/blog\/?p=12222"},"modified":"2023-05-09T09:32:04","modified_gmt":"2023-05-09T09:32:04","slug":"php-mysql-last-inserted-id","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/","title":{"rendered":"PHP MySQL : Last Inserted ID"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">Cara Mendapatkan ID Last Inserted Row<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Dalam bab&nbsp;<em>PHP MySQL insert<\/em>, kalian telah belajar bahwa MySQL secara otomatis menghasilkan ID unik untuk kolom&nbsp;<strong><em>AUTO_INCREMENT<\/em><\/strong>&nbsp;setiap kali kalian memasukkan catatan atau baris baru ke dalam tabel. Namun, ada situasi tertentu ketika kalian membutuhkan ID yang dihasilkan secara otomatis untuk memasukkannya ke dalam tabel kedua. Dalam situasi ini, kalian dapat menggunakan fungsi PHP&nbsp;<strong><em>mysqli_insert_id()<\/em><\/strong>&nbsp;untuk mengambil ID yang terakhir dibuat, seperti yang ditunjukkan pada contoh yang selanjutnya.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Untuk contoh ini kita akan menggunakan tabel&nbsp;<em>persons<\/em>&nbsp;yang sama yang telah kita buat di bab&nbsp;<em>PHP MySQL membuat tabel<\/em>, yang memiliki empat kolom&nbsp;<em>id, first_name, last_name<\/em>&nbsp;dan&nbsp;<em>email<\/em>, di mana&nbsp;<em>id<\/em>&nbsp;adalah kolom kunci utama dan ditandai dengan bendera&nbsp;<em><strong>AUTO_INCREMENT<\/strong><\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/* Attempt MySQL server connection. Assuming you are running MySQL\nserver with default setting (user &#039;root&#039; with no password) *\/\n$link = mysqli_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;demo&quot;);\n  \n\/\/ Check connection\nif($link === false){\n    die(&quot;ERROR: Could not connect. &quot; . mysqli_connect_error());\n}\n  \n\/\/ Attempt insert query execution\n$sql = &quot;INSERT INTO persons (first_name, last_name, email) VALUES (&#039;Ron&#039;, &#039;Weasley&#039;, &#039;ronweasley@mail.com&#039;)&quot;;\nif(mysqli_query($link, $sql)){\n    \/\/ Obtain last inserted id\n    $last_id = mysqli_insert_id($link);\n    echo &quot;Records inserted successfully. Last inserted ID is: &quot; . $last_id;\n} else{\n    echo &quot;ERROR: Could not able to execute $sql. &quot; . mysqli_error($link);\n}\n  \n\/\/ Close connection\nmysqli_close($link);\n?&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cara Mendapatkan ID Last Inserted Row Dalam bab&nbsp;PHP MySQL insert, kalian telah belajar bahwa MySQL secara otomatis menghasilkan ID unik untuk kolom&nbsp;AUTO_INCREMENT&nbsp;setiap kali kalian memasukkan<\/p>\n","protected":false},"author":1,"featured_media":9023,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[3834,3835,3837,3838,3836,3833],"class_list":["post-12222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-belajar-web-programming","tag-belajar-php-mysql-last-inserted-id","tag-belajar-php-mysql-last-inserted-id-pemula","tag-kursus-php-mysql-last-inserted-id-bandung","tag-kursus-php-mysql-last-inserted-id-jakarta","tag-tutorial-php-mysql-last-inserted-id","tag-tutorial-php-mysql-last-inserted-id-pemula"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP MySQL : Last Inserted ID - WebHozz Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP MySQL : Last Inserted ID - WebHozz Blog\" \/>\n<meta property=\"og:description\" content=\"Cara Mendapatkan ID Last Inserted Row Dalam bab&nbsp;PHP MySQL insert, kalian telah belajar bahwa MySQL secara otomatis menghasilkan ID unik untuk kolom&nbsp;AUTO_INCREMENT&nbsp;setiap kali kalian memasukkan\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-16T05:05:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-09T09:32:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/person\\\/d5f539ad171dc74baaf6a98dfef6fcef\"},\"headline\":\"PHP MySQL : Last Inserted ID\",\"datePublished\":\"2023-01-16T05:05:35+00:00\",\"dateModified\":\"2023-05-09T09:32:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/\"},\"wordCount\":141,\"publisher\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/php_mysql.jpg\",\"keywords\":[\"Belajar PHP MySQL Last Inserted ID\",\"Belajar PHP MySQL Last Inserted ID Pemula\",\"Kursus PHP MySQL Last Inserted ID Bandung\",\"Kursus PHP MySQL Last Inserted ID Jakarta\",\"Tutorial PHP MySQL Last Inserted ID\",\"Tutorial PHP MySQL Last Inserted ID Pemula\"],\"articleSection\":[\"Belajar Web Programming\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/\",\"name\":\"PHP MySQL : Last Inserted ID - WebHozz Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/php_mysql.jpg\",\"datePublished\":\"2023-01-16T05:05:35+00:00\",\"dateModified\":\"2023-05-09T09:32:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/php_mysql.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/php_mysql.jpg\",\"width\":750,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/php-mysql-last-inserted-id\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP MySQL : Last Inserted ID\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\",\"name\":\"WebHozz Blog\",\"description\":\"Kursus Web &amp; Android di Jakarta Bandung\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#organization\",\"name\":\"WebHozz\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/logo-persegi.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/logo-persegi.jpg\",\"width\":442,\"height\":442,\"caption\":\"WebHozz\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/#\\\/schema\\\/person\\\/d5f539ad171dc74baaf6a98dfef6fcef\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/www.webhozz.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP MySQL : Last Inserted ID - WebHozz Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/","og_locale":"en_US","og_type":"article","og_title":"PHP MySQL : Last Inserted ID - WebHozz Blog","og_description":"Cara Mendapatkan ID Last Inserted Row Dalam bab&nbsp;PHP MySQL insert, kalian telah belajar bahwa MySQL secara otomatis menghasilkan ID unik untuk kolom&nbsp;AUTO_INCREMENT&nbsp;setiap kali kalian memasukkan","og_url":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/","og_site_name":"WebHozz Blog","article_published_time":"2023-01-16T05:05:35+00:00","article_modified_time":"2023-05-09T09:32:04+00:00","og_image":[{"width":750,"height":400,"url":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/"},"author":{"name":"admin","@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/person\/d5f539ad171dc74baaf6a98dfef6fcef"},"headline":"PHP MySQL : Last Inserted ID","datePublished":"2023-01-16T05:05:35+00:00","dateModified":"2023-05-09T09:32:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/"},"wordCount":141,"publisher":{"@id":"https:\/\/www.webhozz.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg","keywords":["Belajar PHP MySQL Last Inserted ID","Belajar PHP MySQL Last Inserted ID Pemula","Kursus PHP MySQL Last Inserted ID Bandung","Kursus PHP MySQL Last Inserted ID Jakarta","Tutorial PHP MySQL Last Inserted ID","Tutorial PHP MySQL Last Inserted ID Pemula"],"articleSection":["Belajar Web Programming"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/","url":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/","name":"PHP MySQL : Last Inserted ID - WebHozz Blog","isPartOf":{"@id":"https:\/\/www.webhozz.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#primaryimage"},"image":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg","datePublished":"2023-01-16T05:05:35+00:00","dateModified":"2023-05-09T09:32:04+00:00","breadcrumb":{"@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#primaryimage","url":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg","contentUrl":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2017\/12\/php_mysql.jpg","width":750,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/blog\/php-mysql-last-inserted-id\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP MySQL : Last Inserted ID"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/blog\/#website","url":"https:\/\/www.webhozz.com\/blog\/","name":"WebHozz Blog","description":"Kursus Web &amp; Android di Jakarta Bandung","publisher":{"@id":"https:\/\/www.webhozz.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webhozz.com\/blog\/#organization","name":"WebHozz","url":"https:\/\/www.webhozz.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2018\/04\/logo-persegi.jpg","contentUrl":"https:\/\/www.webhozz.com\/blog\/wp-content\/uploads\/2018\/04\/logo-persegi.jpg","width":442,"height":442,"caption":"WebHozz"},"image":{"@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.webhozz.com\/blog\/#\/schema\/person\/d5f539ad171dc74baaf6a98dfef6fcef","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bff35e4083f3870e2f911c4437e788147d340f274268d361dd7e1cf20bebb156?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.webhozz.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/12222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/comments?post=12222"}],"version-history":[{"count":2,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/12222\/revisions"}],"predecessor-version":[{"id":12593,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/posts\/12222\/revisions\/12593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/media\/9023"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/media?parent=12222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/categories?post=12222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/blog\/wp-json\/wp\/v2\/tags?post=12222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}