title: PHP PDO-MYSQL : How to use database connection across different classes
body: <p>I'm kinda new to PDO with MYSQL, here are my two files:</p>
  
  <p>I have a connection class that I use to connect to the database:</p>
  
  <pre><code>class connection{
  
  private $host = 'localhost';
  private $dbname = 'devac';
  private $username = 'root';
  private $password ='';  
  
  public $con = '';
  
  function __construct(){
  
      $this-&gt;connect();   
  
  }
  
  function connect(){
  
      try{
  
          $this-&gt;con = new PDO("mysql:host=$this-&gt;host;dbname=$this-&gt;dbname",$this-&gt;username, $this-&gt;password);
          $this-&gt;con-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  
  
      }catch(PDOException $e){
  
          echo 'We\'re sorry but there was an error while trying to connect to the database';
          file_put_contents('connection.errors.txt', $e-&gt;getMessage().PHP_EOL,FILE_APPEND);
  
      }
  }   
  }
  </code></pre>
  
  <p>I have an account_info class that i use to query the data from the database:</p>
  
  <pre><code>class account_info{
  
  
  function getAccountInfo(){
  
      $acc_info = $this-&gt;con-&gt;prepare("SELECT * FROM account_info");
      $acc_info-&gt;execute();
  
      $results = $acc_info-&gt;fetchAll(PDO::FETCH_OBJ);
  
      foreach ($results as $key) {
          $results-&gt;owner_firstname;
  
      }
  }       
  
  
  }
  </code></pre>
  
  <p>I include both these files in my index.php page:</p>
  
  <pre><code>include_once 'classes/connection.class.php';
  include_once 'classes/accountinfo.class.php';
  
  $con = new connection();
  $info = new account_info();
  $info-&gt;getAccountInfo();
  </code></pre>
  
  <p>I just cant get it to work I'm not getting any output, I think it has something to do with the scope, but I don't know the correct why to fix it as I'm new to this PDO and OOP stuff. 
  Thanks in advance.</p>