title: php/pdo/msql - access denied
body: <p>There is a little pdo problem, that I have been working on for a while now. Since I don't know what is wrong here, I thought about taking it to this list. Maybe some of you know more...</p>
  
  <p>I have a website with a login that checks a user and a password against a mysql driven database. When the pdo connection is made in the same file all works fine, one can log in, without any problems. Just as it is supposed to work...</p>
  
  <p>However, when moving the database connection part to a seperate function, which I include from another file, pdo fails on me, and gives me:     </p>
  
  <blockquote>
    <p>SQLSTATE[28000] [1045] Access denied for user '...'@'...' (using
    password: NO)
        Fatal error: Call to a member function prepare() on a non-object in /.../.../... on line 41</p>
  </blockquote>
  
  <p>For the sake of clarity, here is the code:</p>
  
  <p><strong>Version 1:</strong></p>
  
  <p>This works:</p>
  
  <pre><code>&lt;?php
      require "./vars_and_functions.php";
  
      /* open database connection */
      try {
      $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  
      /* query */
      $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
      $q = $pdo-&gt;prepare($query);
      $q-&gt;execute(array($u_name, $p_word_md5));
      $result = $q-&gt;rowCount();
  
      if($result == 1) { /* we have a match */                       
      /* close the database connection */
                 $pdo = null;  
  
                  /* and redirect */
                  header("...");
  
      } /* if */
      else { /* wrong credentials */
          /* close the database connection */                
                  $pdo = null;
  
                  /* and go back to the login page */ 
          header("...");
      } /* else */
          } /* try */ 
      catch(PDOException $e) {  
          echo $e-&gt;getMessage();  
      } /* catch */
  ?&gt;
  </code></pre>
  
  <p><strong>Here is version 2</strong></p>
  
  <p>This does not work:</p>
  
  <pre><code>&lt;?php
          require "./vars_and_functions.php";
  
          /* open database connection */
          $pdo = database_connection();
  
  
  
      /* query */
              $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
              $q = $pdo-&gt;prepare($query);
              $q-&gt;execute(array($u_name, $p_word_md5));
              $result = $q-&gt;rowCount();
  
          if($result == 1) { /* we have a match */                       
                 /* close the database connection */
                     $pdo = null;  
  
                      /* and redirect */
                      header("...");
  
          } /* if */
          else { /* wrong credentials */
              /* close the database connection */                
                      $pdo = null;
  
                      /* and go back to the login page */ 
              header("...");
          } /* else */
              } /* try */ 
      catch(PDOException $e) {  
          echo $e-&gt;getMessage();  
      } /* catch */
  ?&gt;
  </code></pre>
  
  <p>My includefile vars_and_functions.php looks like this:</p>
  
  <pre><code>$db_host = "...";       
  $db_name = "...";           
  $db_user = "...";       
  $db_pass = "...";       
  
  function database_connection() {
      try {
      $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  }  
  catch(PDOException $e) {  
      echo $e-&gt;getMessage();  
  }  
  
  return $pdo;
  }
  </code></pre>
  
  <p>The only real difference to my mind is that here, the pdo connection is made via a function call, whereas the function sits in the include file vars_and_functions.php.</p>
  
  <p>What's wrong here?</p>