title: How to share MySQL connection over class in PHP?
body: <p>I want to share a MySQL connection, with other class within PHP, which held the query to do.
  So MySQL.php contains:</p>
  
  <pre><code>    &lt;?php
  $user = 'username';
  $passowrd = 'password';
  $conn = new PDO('mysql:host=ip;dbname=DBName;port=3306',$user,$passowrd);
  ?&gt;
  </code></pre>
  
  <p>And the php file, which I call MyLibrary.php contains:</p>
  
  <pre><code>    &lt;?php
  include 'DB.php';
  
  class Manager {
  
      function jsonQuery() {
  
          $response = array ();
  
  
          $st = $conn-&gt;query ( "query");
  
          $response ["results"] = array ();
  
          foreach ( $st-&gt;fetchAll () as $row ) {
  
              //my stuff with the query
          }
  
          $response ["success"] = 1;
  
          echo json_encode ( $response );
      }
  
  }
  
  $object = new Manager();
  
  $object-&gt;jsonQuery();
  
  ?&gt;
  </code></pre>
  
  <p>So how can I do ? </p>