{"id":1240,"date":"2022-08-30T15:14:43","date_gmt":"2022-08-30T15:14:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/php-databases-pdo-connections-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:43","modified_gmt":"2022-08-30T15:14:43","slug":"php-databases-pdo-connections-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-databases-pdo-connections-collection-of-common-programming-errors\/","title":{"rendered":"PHP Databases PDO connections-Collection of common programming errors"},"content":{"rendered":"<p>Hey guys im having a little trouble with the PDO in php as the error it is returning is an undefined index. The code for the function and query and return of result is this:<\/p>\n<pre><code>function getUserDetails($user) {\n   $db = connect();\ntry {\n    $stmt = $db-&gt;prepare('SELECT name,addr AS address,team\nFROM TreasureHunt.Player LEFT OUTER JOIN TreasureHunt.MemberOf ON (name=player) \nLEFT OUTER JOIN TreasureHunt.PlayerStats USING (player)\nWHERE name=:user');\n\n    $stmt-&gt;bindValue(':user', $user, PDO::PARAM_STR);\n\n    $stmt-&gt;execute();\n    $results = $stmt-&gt;fetchAll();\n    $stmt-&gt;closeCursor();\n\n} catch (PDOException $e) { \n    print \"Error : \" . $e-&gt;getMessage(); \n    die();\n}\nreturn $results;  \n}\n<\/code><\/pre>\n<p>However when running the code for the index page i get an error that says Notice: Undefined index: name<\/p>\n<p>The Code for the index is this:<\/p>\n<pre><code>try {\n$details = getUserDetails($_SESSION['player']);\necho '<\/code><\/pre>\n<h2><code>Name<\/code><\/h2>\n<pre> ',$details['name'];\necho '<\/pre>\n<h2><code>Address<\/code><\/h2>\n<pre>',$details['address'];\necho '<\/pre>\n<h2><code>Current team<\/code><\/h2>\n<pre>',$details['team'];\necho '<\/pre>\n<h2><code>Hunts played<\/code><\/h2>\n<pre> ',$details['nhunts'];\necho '<\/pre>\n<h2><code>Badges<\/code><\/h2>\n<pre>';\nforeach($details['badges'] as $badge) {\n    echo '',$badge['name'],'<br \/>';\n}\n} catch (Exception $e) {\necho 'Cannot get user details';\n}\n<\/pre>\n<p><code>my question is why is it throwing a notice and how do i go around this problem?<\/code><\/p>\n<ol>\n<li>\n<p><code><code>fetchAll<\/code> returns <em>all<\/em> results (potentially multiple rows) in a <em>multidimensional array<\/em>:<\/code><\/p>\n<pre><code>array(\n    0 =&gt; array(\/* first row *\/),\n    1 =&gt; array(\/* second row *\/),\n    ...\n)\n<\/code><\/pre>\n<p>That&#8217;s why the array doesn&#8217;t have a direct index <code>'name'<\/code>, it needs to be <code>[0]['name']<\/code>.<br \/>\nOr you shouldn&#8217;t <code>fetchAll<\/code>, just <code>fetch<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Hey guys im having a little trouble with the PDO in php as the error it is returning is an undefined index. The code for the function and query and return of result is this: function getUserDetails($user) { $db = connect(); try { $stmt = $db-&gt;prepare(&#8216;SELECT name,addr AS address,team FROM TreasureHunt.Player LEFT OUTER JOIN TreasureHunt.MemberOf [&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-1240","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1240","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=1240"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}