{"id":7250,"date":"2014-06-03T22:50:38","date_gmt":"2014-06-03T22:50:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/unexpected-result-using-pdo-in-php-collection-of-common-programming-errors\/"},"modified":"2014-06-03T22:50:38","modified_gmt":"2014-06-03T22:50:38","slug":"unexpected-result-using-pdo-in-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/unexpected-result-using-pdo-in-php-collection-of-common-programming-errors\/","title":{"rendered":"Unexpected result using PDO in php-Collection of common programming errors"},"content":{"rendered":"<p>By default, PDO will grab a numeric AND an associative array when fetching data. To ensure you only get 1 of the 2, you can specify it as the second parameter in your query function by using either <code>PDO::FETCH_ASSOC<\/code> or <code>PDO::FETCH_NUMBER<\/code>.<\/p>\n<p>Change<\/p>\n<pre><code>foreach($dbc-&gt;query('SELECT * FROM `test` ') as $row)\n<\/code><\/pre>\n<p>into<\/p>\n<pre><code>\/\/ Fetch associative\nforeach($dbc-&gt;query('SELECT * FROM `test` ', PDO::FETCH_ASSOC) as $row)\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>\/\/ Fetch numeric\nforeach($dbc-&gt;query('SELECT * FROM `test` ', PDO::FETCH_NUMBER) as $row)\n<\/code><\/pre>\n<p><strong>[edit]<\/strong><br \/>\nYou can also set the default fetch mode after you created your PDO connection by doing the following:<\/p>\n<pre><code>\/\/ Set default fetch mode to PDO::FETCH_ASSOC\n$dbc-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n<\/code><\/pre>\n<p>When doing that, doing<\/p>\n<pre><code>foreach($dbc-&gt;query('SELECT * FROM `test` ')\n<\/code><\/pre>\n<p>will do exactly the same as<\/p>\n<pre><code>foreach($dbc-&gt;query('SELECT * FROM `test` ', PDO::FETCH_ASSOC) as $row)\n<\/code><\/pre>\n<p>Hope this helped.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, PDO will grab a numeric AND an associative array when fetching data. To ensure you only get 1 of the 2, you can specify it as the second parameter in your query function by using either PDO::FETCH_ASSOC or PDO::FETCH_NUMBER. Change foreach($dbc-&gt;query(&#8216;SELECT * FROM `test` &#8216;) as $row) into \/\/ Fetch associative foreach($dbc-&gt;query(&#8216;SELECT * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7250","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7250"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7250\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}