title: PHP: class display error function
body: <p>Is it something incomplete/ incorrect in the error class function below? I can never get any error message from it when a query is incorrect,</p>
  
  <pre><code>#connects the database and handling the result
  class __database {
  
      protected $connection = null;
      protected $error = null;
  
      #make a connection
      public function __construct($hostname,$username,$password,$database)
      {
          $this -&gt; connection = new mysqli($hostname,$username,$password,$database);
  
          if (mysqli_connect_errno()) 
          {
              printf("Connect failed: %s\n", mysqli_connect_error());
              exit();
          }
      }
  
      #fetches all result rows as an associative array, a numeric array, or both
      public function fetch_all($query) 
      {
          $result = $this -&gt; connection -&gt; query($query);
          if($result) 
          {
  
              return $result -&gt; fetch_all(MYSQLI_ASSOC);
          } 
          else
          {
              $this -&gt; error = $this -&gt; connection -&gt; error;
              return $this -&gt; error;
          }
      }
  
      #display error
      public function get_error() 
      {
          return $this -&gt; error;
      }
  
      #closes the database connection when object is destroyed.
      public function __destruct()
      {
          $this -&gt; connection -&gt; close();
      }
  }
  </code></pre>
  
  <p><code>public function get_error()</code> seems to be useless in my db class... I have read about php <code>Exception</code> but I don't know how to incorporate it into the this db class above! please advise...</p>
  
  <p>EDIT:</p>
  
  <p>I tried to change the code into this,</p>
  
  <pre><code># return the current row of a result set as an object
      public function fetch_object($query) 
      {
          $result = $this-&gt;connection-&gt;query($query);
          if($result)
          {
              ...
          }
          else
          {
              __database::get_error();
          }
      }
  </code></pre>
  
  <p>and the error class function,</p>
  
  <pre><code>#display error
      public function get_error() 
      {
          $this-&gt;error = $this-&gt;connection-&gt;error;
          return $this-&gt;error;
      }
  </code></pre>
  
  <p>So I thought it should trigger the get_error() function, but still nothing has been displayed from the error function...</p>