Creating a Database connection class (PDO) and fetch data
<p>I am new to OOP so I am trying to learn how to create classes and use them. Currently I am trying to fetch data from my mysql table. </p>

<p>To create the connection with mysql I am using PDO. I have created a separate class for database connection. I have included the class in my show.php file. Now I want to fetch data from mysql database. The problem is when I run my show.php file it shows this error message <code>Fatal error: Call to undefined method DBConnection::prepare() in C:\xampp\htdocs\jm\show.php on line 10</code> but it was supposed to display just <code>array</code>. </p>

<p>Could you please help me find the solution to this problem? </p>

<p>db.class.php</p>

<pre><code>&lt;?php
class DBConnection {

function DBConnection(){

    $host='localhost'; 
    $dbname='srijon';
    $user='root';
    $pass='';

    try {  
      $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
      $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  

        return $DBH;
    }
    catch(PDOException $e) {  

     echo 'ERROR: ' . $e-&gt;getMessage();
    }   

}//function ends

}// class ends
?&gt;
</code></pre>

<p>show.php</p>

<pre><code>&lt;?php
 require_once 'db.class.php';

  function get_all(){

  $db=new DBConnection();

  $sql="SELECT * FROM information";
  $STH = $db-&gt;prepare($sql);
  $STH-&gt;execute();
  $STH-&gt;setFetchMode(PDO::FETCH_ASSOC);

  return $STH;

  }

  echo get_all();

 ?&gt;
</code></pre>
