title: how to use PDO connection object in different files
body: <p>Hello i am new to PDO with MYSQL, here are my two files
   <strong>1) index.php</strong></p>
  
  <pre><code>require_once 'prd.php';
  try{
      $db = new PDO ('mysql:host=xxxx;dbname=xxx;charset=utf8', 'xxx', 'xxxx');
      echo 'connectd';
  }catch(PDOException $conError){
      echo 'failed to connect DB' . $conError-&gt;getMessage ();
  }
  $conn = new prdinfo();
  $conn-&gt;con($db);
  </code></pre>
  
  <p><strong>2) product.php</strong></p>
  
  <pre><code>class prdinfo{function con($db){
      try{
          foreach($db-&gt;query("select * from products where vendor_id = 2" ) as $row){
              $prod_id = $row ['product_id'];
              echo '&lt;br/&gt;' . $prod_id;
          }
      }catch(PDOException $ex){
          echo 'an error occured' . $ex-&gt;getMessage();
      }
  }
  }
  </code></pre>
  
  <p>my problem is here i can pass the connection object to every file, but  i have so many files to use database queries, so i need to pass the $bd to all the files. this is getting burden on the code. so is there any way to connect the database with PDO.
  <strong>Thanks</strong></p>