title: PHP: Proper way of using a PDO database connection in a class
body: <p>Trying to organize all my code into classes, and I can't get the database queries to work inside a class. I tested it without the class wrapper, and it worked fine. Inside the class = no dice. <strong>What about my classes is messing this up?</strong></p>
  
  <p><strong>EDIT:</strong> The problem is that it won't query the database and return a result. Any result. </p>
  
  <pre><code>class ac
    {
    public function dbConnect() 
      {
      global $dbcon;
  
      $dbInfo['server'] = "localhost";
      $dbInfo['database'] = "sn";
      $dbInfo['username'] = "sn";
      $dbInfo['password'] = "password"; 
  
      $con = "mysql:host=" . $dbInfo['server'] . "; dbname=" . $dbInfo['database'];
      $dbcon = new PDO($con, $dbInfo['username'], $dbInfo['password']);
      $dbcon-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $error = $dbcon-&gt;errorInfo();
  
      if($error[0] != "") 
        {
        print "&lt;p&gt;DATABASE CONNECTION ERROR:&lt;/p&gt;";
        print_r($error);
        }
      }
  
  
    public function authentication()
      {
      global $dbcon;
  
      $plain_username = $_POST['username'];
      $md5_password = md5($_POST['password']);
  
      $ac = new ac();
      if (is_int($ac-&gt;check_credentials($plain_username, $md5_password)))
        {
        ?&gt;
        &lt;p&gt;Welcome!&lt;/p&gt; &lt;!--go to account manager here--&gt;
        &lt;?php
        }
      else
        {
        ?&gt;
        &lt;p&gt;Not a valid username and/or password. Please try again.&lt;/p&gt;
        &lt;?php
        unset($_POST['username']);
        unset($_POST['password']);
        $ui = new ui();
        $ui-&gt;start();
        }
      }
  
  
  
    private function check_credentials($plain_username, $md5_password)
      {
      global $dbcon;
  
      $userid = $dbcon-&gt;prepare('SELECT id FROM users WHERE username = :username AND password = :password LIMIT 1');
      $userid-&gt;bindParam(':username', $plain_username);
      $userid-&gt;bindParam(':password', $md5_password);
      $userid-&gt;execute();
  
      print_r($dbcon-&gt;errorInfo());
  
      $id = $userid-&gt;fetch();
      Return $id;
      }
    }
  </code></pre>
  
  <p>And if it's any help, here's the class that's calling it:</p>
  
  <pre><code>require_once("ac/acclass.php");
  $ac = new ac();
  $ac-&gt;dbconnect();
  class ui
    {
    public function start()
      {
      if ((!isset($_POST['username'])) &amp;&amp; (!isset($_POST['password'])))
        {
        $ui = new ui();
        $ui-&gt;loginform();
        }
      else
        {
        $ac = new ac();
        $ac-&gt;authentication();
        }
      }
  
    private function loginform()
      {
      ?&gt;
      &lt;form id="userlogin" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
       User:&lt;input type="text" name="username"/&gt;&lt;br/&gt;
       Password:&lt;input type="password" name="password"/&gt;&lt;br/&gt;
       &lt;input type="submit" value="submit"/&gt;
      &lt;/form&gt;
      &lt;?php
      }
    }
  </code></pre>