title: How to access a connect function from another file in another function
body: <p>The problem here is that the viewauthor.php considered the <code>$conn</code> as an undefined variable. It also gives me an error that states <code>mysqli_query() expects parameter 1 to be mysqli, null given in</code> in the <code>$results</code> line. How can I make the viewauthor.php access the <code>$conn</code> in the dbconnect.php file? </p>
  
  <p>this is the code in db.connect.php</p>
  
  <pre><code>   &lt;?php
      class dbconn {
      public function dbcon() {
          global $conn;
          $conn = new mysqli('localhost','root','','bookinventory') or die 
          ('Error connecting to mysql' .mysqli_error());
          return $conn;
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>this is the code in viewauthor.php</p>
  
  <pre><code>    &lt;?php
      require_once 'dbconnect.php';
  
      class author {
       $con = new dbconn();
       $conn = $con-&gt;dbcon();
  
      public function viewAuthor() {  
      $query = ("SELECT * FROM author");
      $results = mysqli_query($conn,$query);
      $authors = array();
  
              while($author = mysqli_fetch_object($results)) {
              $authors[] = $author;
        }
              return $authors;
      }
   }
   ?&gt;
  </code></pre>