{"id":2146,"date":"2021-07-07T09:54:51","date_gmt":"2021-07-07T09:54:51","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2146"},"modified":"2021-07-07T10:30:08","modified_gmt":"2021-07-07T10:30:08","slug":"pdo-fetch_key_pair","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/pdo-fetch_key_pair\/","title":{"rendered":"PDO FETCH_KEY_PAIR"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PDO <code>FETCH_KEY_PAIR<\/code> mode to select data from a table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pdo-fetch_key_pair-mode'>Introduction to the PDO FETCH_KEY_PAIR mode <a href=\"#introduction-to-the-pdo-fetch_key_pair-mode\" class=\"anchor\" id=\"introduction-to-the-pdo-fetch_key_pair-mode\" title=\"Anchor for Introduction to the PDO FETCH_KEY_PAIR mode\">#<\/a><\/h2>\n\n\n\n<p>Both <code>fetch()<\/code> and fetchAll() methods accept a very useful fetch mode called <code>PDO::FETCH_KEY_PAIR<\/code>.<\/p>\n\n\n\n<p>The <code>PDO::FETCH_KEY_PAIR<\/code> mode allows you to retrieve a two-column result in an array where the first column is the key and the second column is the value.<\/p>\n\n\n\n<p>In practice, you&#8217;ll use the <code>PDO::FETCH_KEY_PAIR<\/code> to fetch data for constructing a <code>&lt;select><\/code> element with data that comes from the database.<\/p>\n\n\n\n<p>For example, you can create a <code>&lt;select><\/code> element with the values are publisher id and texts are publisher names:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n$sql = <span class=\"hljs-string\">'SELECT publisher_id, name \n        FROM publishers'<\/span>;\n\n$statement = $pdo-&gt;query($sql);\n$publishers = $statement-&gt;fetchAll(PDO::FETCH_KEY_PAIR);\n<span class=\"hljs-meta\">?&gt;<\/span>\n&lt;!DOCTYPE html&gt;\n&lt;html lang=<span class=\"hljs-string\">\"en\"<\/span>&gt;\n&lt;head&gt;\n    &lt;meta charset=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;\n    &lt;meta name=<span class=\"hljs-string\">\"viewport\"<\/span> content=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span>&gt;\n    &lt;title&gt;Publishers&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"publisher\"<\/span>&gt;Select a pulisher&lt;\/label&gt;\n    &lt;select name=<span class=\"hljs-string\">\"publisher\"<\/span> id=<span class=\"hljs-string\">\"publisher\"<\/span>&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">foreach<\/span> ($publishers <span class=\"hljs-keyword\">as<\/span> $publisher_id =&gt; $name): <span class=\"hljs-meta\">?&gt;<\/span>\n        &lt;option value=<span class=\"hljs-string\">\"&lt;?php echo $publisher_id ?&gt;\"<\/span>&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> $name <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/option&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endforeach<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;\/select&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<div class=\"output-cont\">\n<div class=\"output\">\n    <label for=\"publisher\">Select a pulisher<\/label>\n    <select name=\"publisher\" id=\"publisher\">\n                <option value=\"1\">McGraw-Hill Education<\/option>\n                <option value=\"2\">Penguin\/Random House<\/option>\n                <option value=\"3\">Hachette Book Group<\/option>\n                <option value=\"4\">Harper Collins<\/option>\n                <option value=\"5\">Simon and Schuster<\/option>\n            <\/select>\n<\/div>\n<\/div>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, connect to the <code>bookdb<\/code> database.<\/p>\n\n\n\n<p>Second, execute a query that selects the publisher id and name from the <code>publishers<\/code> table using the <code>query()<\/code> function.<\/p>\n\n\n\n<p>Third, fetch all rows from the result set using the <code>PDO::FETCH_KEY_PAIR<\/code> mode. The <code>$publishers<\/code> array will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; McGraw-Hill Education\n    &#91;<span class=\"hljs-number\">2<\/span>] =&gt; Penguin\/Random House \n    &#91;<span class=\"hljs-number\">3<\/span>] =&gt; Hachette Book Group\n    &#91;<span class=\"hljs-number\">4<\/span>] =&gt; Harper Collins\n    &#91;<span class=\"hljs-number\">5<\/span>] =&gt; Simon <span class=\"hljs-keyword\">and<\/span> Schuster\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, iterate over the result set and create the option elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>PDO::FETCH_KEY_PAIR<\/code> mode to fetch the two-column result in an array where the first column is the key and the second column is the value.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2146\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/pdo-fetch_key_pair\/\"\n\t\t\t\tdata-post-title=\"PDO FETCH_KEY_PAIR\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2146\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/pdo-fetch_key_pair\/\"\n\t\t\t\tdata-post-title=\"PDO FETCH_KEY_PAIR\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PDO FETCH_KEY_PAIR mode to select data from a table. Introduction to the PDO FETCH_KEY_PAIR mode # Both fetch() and fetchAll() methods accept a very useful fetch mode called PDO::FETCH_KEY_PAIR. The PDO::FETCH_KEY_PAIR mode allows you to retrieve a two-column result in an array where the first [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2146","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2146"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2146\/revisions"}],"predecessor-version":[{"id":2150,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2146\/revisions\/2150"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}