{"id":538,"date":"2019-09-20T08:22:50","date_gmt":"2019-09-20T08:22:50","guid":{"rendered":"https:\/\/www.webhozz.com\/code\/?p=538"},"modified":"2019-09-20T08:22:52","modified_gmt":"2019-09-20T08:22:52","slug":"php-mysql-order-by","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/","title":{"rendered":"PHP MySQL : Order By"},"content":{"rendered":"\n<p>Klausa <strong><em>ORDER BY<\/em><\/strong> dapat digunakan dalam konjugasi dengan pernyataan <strong><em>SELECT<\/em><\/strong> untuk melihat data dari tabel yang dipesan oleh bidang tertentu. Klausa <strong><em>ORDER BY<\/em><\/strong> memungkinkan kalian menentukan nama bidang untuk disortir dan arah pengurutan ascending atau descending .<\/p>\n\n\n\n<p>Sintaks dasar klausa ini dapat diberikan dengan:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img fetchpriority=\"high\" decoding=\"async\" width=\"784\" height=\"72\" src=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg\" alt=\"\" class=\"wp-image-539\" srcset=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg 784w, https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32-300x28.jpg 300w, https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32-768x71.jpg 768w\" sizes=\"(max-width: 784px) 100vw, 784px\" \/><\/figure>\n\n\n\n<p>Mari kita membuat query SQL menggunakan klausa <strong><em>ORDER BY<\/em><\/strong> dalam pernyataan <strong><em>SELECT<\/em><\/strong>, setelah itu kita akan mengeksekusi query ini dengan mengirimkannya ke fungsi PHP <strong><em>mysqli_query()<\/em><\/strong> untuk mendapatkan data yang di ordered. Pertimbangkan tabel berikut <em>persons <\/em>di dalam basis data <em>demo<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----+------------+-----------+----------------------+\n| id | first_name | last_name | email                |\n+----+------------+-----------+----------------------+\n|  1 | Peter      | Parker    | peterparker@mail.com |\n|  2 | John       | Rambo     | johnrambo@mail.com   |\n|  3 | Clark      | Kent      | clarkkent@mail.com   |\n|  4 | John       | Carter    | johncarter@mail.com  |\n|  5 | Harry      | Potter    | harrypotter@mail.com |\n+----+------------+-----------+----------------------+<\/pre>\n\n\n\n<p>Kode PHP dalam contoh berikut memilih semua baris dari tabel <em>persons <\/em>dan mengurutkan hasilnya dengan kolom <em>first_name<\/em> dalam urutan ascending alfabet.<\/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 'root' 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 select query execution with order by clause\n$sql = &quot;SELECT * FROM persons ORDER BY first_name&quot;;\nif($result = mysqli_query($link, $sql)){\n    if(mysqli_num_rows($result) &gt; 0){\n        echo &quot;&lt;table&gt;&quot;;\n            echo &quot;&lt;tr&gt;&quot;;\n                echo &quot;&lt;th&gt;id&lt;\/th&gt;&quot;;\n                echo &quot;&lt;th&gt;first_name&lt;\/th&gt;&quot;;\n                echo &quot;&lt;th&gt;last_name&lt;\/th&gt;&quot;;\n                echo &quot;&lt;th&gt;email&lt;\/th&gt;&quot;;\n            echo &quot;&lt;\/tr&gt;&quot;;\n        while($row = mysqli_fetch_array($result)){\n            echo &quot;&lt;tr&gt;&quot;;\n                echo &quot;&lt;td&gt;&quot; . $row&#x5B;'id'] . &quot;&lt;\/td&gt;&quot;;\n                echo &quot;&lt;td&gt;&quot; . $row&#x5B;'first_name'] . &quot;&lt;\/td&gt;&quot;;\n                echo &quot;&lt;td&gt;&quot; . $row&#x5B;'last_name'] . &quot;&lt;\/td&gt;&quot;;\n                echo &quot;&lt;td&gt;&quot; . $row&#x5B;'email'] . &quot;&lt;\/td&gt;&quot;;\n            echo &quot;&lt;\/tr&gt;&quot;;\n        }\n        echo &quot;&lt;\/table&gt;&quot;;\n        \/\/ Close result set\n        mysqli_free_result($result);\n    } else{\n        echo &quot;No records matching your query were found.&quot;;\n    }\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>Setelah ordering hasilnya, hasil set-nya akan terlihat seperti ini:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----+------------+-----------+----------------------+\n| id | first_name | last_name | email                |\n+----+------------+-----------+----------------------+\n|  3 | Clark      | Kent      | clarkkent@mail.com   |\n|  5 | Harry      | Potter    | harrypotter@mail.com |\n|  2 | John       | Rambo     | johnrambo@mail.com   |\n|  4 | John       | Carter    | johncarter@mail.com  |\n|  1 | Peter      | Parker    | peterparker@mail.com |\n+----+------------+-----------+----------------------+<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Klausa ORDER BY dapat digunakan dalam konjugasi dengan pernyataan SELECT untuk melihat data dari tabel yang dipesan oleh bidang tertentu. Klausa ORDER BY memungkinkan kalian<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-538","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Belajar PHP MySQL : Order By - WebHozz Code<\/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\/code\/php-mysql-order-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Belajar PHP MySQL : Order By - WebHozz Code\" \/>\n<meta property=\"og:description\" content=\"Klausa ORDER BY dapat digunakan dalam konjugasi dengan pernyataan SELECT untuk melihat data dari tabel yang dipesan oleh bidang tertentu. Klausa ORDER BY memungkinkan kalian\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Code\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-20T08:22:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-20T08:22:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"784\" \/>\n\t<meta property=\"og:image:height\" content=\"72\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"headline\":\"PHP MySQL : Order By\",\"datePublished\":\"2019-09-20T08:22:50+00:00\",\"dateModified\":\"2019-09-20T08:22:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/\"},\"wordCount\":119,\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-32.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/\",\"name\":\"Belajar PHP MySQL : Order By - WebHozz Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-32.jpg\",\"datePublished\":\"2019-09-20T08:22:50+00:00\",\"dateModified\":\"2019-09-20T08:22:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-32.jpg\",\"contentUrl\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/Untitled-1-32.jpg\",\"width\":784,\"height\":72},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-mysql-order-by\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP MySQL : Order By\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\",\"name\":\"WebHozz Code\",\"description\":\"Tutorial Web &amp; Pemrograman Indonesia\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/author\\\/dody\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Belajar PHP MySQL : Order By - WebHozz Code","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\/code\/php-mysql-order-by\/","og_locale":"en_US","og_type":"article","og_title":"Belajar PHP MySQL : Order By - WebHozz Code","og_description":"Klausa ORDER BY dapat digunakan dalam konjugasi dengan pernyataan SELECT untuk melihat data dari tabel yang dipesan oleh bidang tertentu. Klausa ORDER BY memungkinkan kalian","og_url":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/","og_site_name":"WebHozz Code","article_published_time":"2019-09-20T08:22:50+00:00","article_modified_time":"2019-09-20T08:22:52+00:00","og_image":[{"width":784,"height":72,"url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/"},"author":{"name":"admin","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"headline":"PHP MySQL : Order By","datePublished":"2019-09-20T08:22:50+00:00","dateModified":"2019-09-20T08:22:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/"},"wordCount":119,"image":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg","articleSection":["PHP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/","url":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/","name":"Belajar PHP MySQL : Order By - WebHozz Code","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#primaryimage"},"image":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg","datePublished":"2019-09-20T08:22:50+00:00","dateModified":"2019-09-20T08:22:52+00:00","author":{"@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"breadcrumb":{"@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#primaryimage","url":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg","contentUrl":"https:\/\/www.webhozz.com\/code\/wp-content\/uploads\/2019\/09\/Untitled-1-32.jpg","width":784,"height":72},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/code\/php-mysql-order-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/code\/"},{"@type":"ListItem","position":2,"name":"PHP MySQL : Order By"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/code\/#website","url":"https:\/\/www.webhozz.com\/code\/","name":"WebHozz Code","description":"Tutorial Web &amp; Pemrograman Indonesia","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.webhozz.com\/code\/author\/dody\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":1,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/538\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/538\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/media?parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/categories?post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/tags?post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}