title: Fatal error: Call to a member function prepare() on a non-object in mysqlicon.php on line 62 Help explain OOP mysqli classes and prepared statements
body: <p>I'm hesitant to post this, as I'd really prefer to figure this out myself, but I don't think I will. I'm just trying to set a class for mysqli stuff, to make it as dynamic as possible, and yes I'm newer to OOP, but have been using PHP and Mysql as a hobby, and more heavily lately, for quite some time. I figured it was time to switch, but there just isn't that much on oop classes with mysqli and prepared statements with a possibility of multiple results (yes I've check documentation, guess I'm just not getting it or something). After quite a few hours, this is what I have. I'm not necessarily looking for a "quick fix". I really want to understand this and learn, so please explain thoroughly.</p>
  
  <p>I'm using a dbconfig.php file to store my database info at root/config/dbconfig.php</p>
  
  <p>in root/classes/mysqlicon.php</p>
  
  <pre><code>&lt;?php
  /*
  * class MYSQLIDB
  * @param Host
  * @param User 
  * @param Password
  * @param Name
  */
  class MYSQLIDB
  {
  private $host;       //MySQL Host
  private $user;       //MySQL User
  private $pass;       //MySQL Password
  private $name;       //MySQL Name   
  private $mysqli;     //MySQLi Object
  private $last_query; //Last Query Run
  
  /*
   * Class Constructor
   * Creates a new MySQLi Object
  */
  public function __construct()
  {   
      include('./config/dbconfig.php');
      $this-&gt;host = $db_host;
      $this-&gt;user = $db_user;
      $this-&gt;pass = $db_pass;
      $this-&gt;name = $db_name;
  
      $this-&gt;mysqli = new mysqli($this-&gt;host, $this-&gt;user, $this-&gt;pass, $this-&gt;name);
      if ($mysqli-&gt;connect_errno) {
          return "Failed to connect to MySQLi: (" . $mysqli-&gt;connect_errno . ") " . $mysqli-&gt;connect_error;
      }
  
  }
  
  private function __destruct()
  {   
      $mysqli-&gt;close();
  }
  
  public function select($fields, $from, $where, $whereVal, $type, $orderByVal, $ASDESC, $limitVal)
  {
      if (is_int($whereVal))
      {
          $bindVal = 'i';
      } else {
          $bindVal = 's';
      }
      switch($type)
      {
          case "regular":
              $queryPre = "SELECT " . $fields;
              $querySuff = " WHERE " . $where . " =  ?";
              break;
          case "orderByLimit":
              $queryPre = "SELECT " . $fields;
              $querySuff = " ORDER BY " . $orderByVal . " " . $ASDESC . " LIMIT " . $limitVal;
              break;
      }
      //$query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
      if ($stmt = $mysqli-&gt;prepare('$queryPre . " FROM " . $from . " " . $querySuff')) 
      {
          if ($type == 'regular') {
              $stmt-&gt;bind_param($bindVal, $whereVal);
          }
          $stmt-&gt;execute(); 
          $stmt-&gt;bind_result($values); 
          $stmt-&gt;store_result(); 
          $sr = new Statement_Result($stmt);      
          $stmt-&gt;fetch(); 
          // call by this style printf("ID: %d\n", $sr-&gt;Get('id') ); 
          //$stmt-&gt;fetch(); 
          //$stmt-&gt;close(); 
          //return $value;
          printf("ID: %d\n", $sr-&gt;Get_Array() );
      } else return null; 
  }
  //use to call $db = new MYSQLI('localhost', 'root', '', 'blog');
  /*
   * Function Select
   * @param fields
   * @param from
   * @param where
   * @returns Query Result Set
  
  function select($fields, $from, $where, $orderBy, $ASDESC, $limit, varNamesSent)
  {   
      if ($orderBy == null &amp;&amp;)
      $query = "SELECT " . $fields . " FROM `" . $from . "` WHERE " . $where;
      $result = $this-&gt;mysqli-&gt;query($query) or exit("Error code ({$sql-&gt;errno}): {$sql-&gt;error}");
  
      $this-&gt;last_query = $query;
  
      return $result;
  }
  
  /*
   * Function Insert
   * @param into
   * @param values
   * @returns boolean
  */
  public function insert($into, $values)
  {
      $query = "INSERT INTO " . $into . " VALUES(" . $values . ")";
  
      $this-&gt;last_query = $query;
  
      if($this-&gt;mysqli-&gt;query($query))
      {
          return true;
      } else {
          return false;
      }
  
  }
  
  /*
   * Function Delete
   * @param from
   * @param where
   * @returns boolean
  */
  public function delete($from, $where)
  {
      $query = "DELETE FROM " . $from . " WHERE " . $where;
  
      $this-&gt;last_query = $query;
  
      if($this-&gt;mysqli-&gt;query($query))
      {
          return true;
      } else {
          return false;
      }
  }
  }
  //Hand arrays for multiple returned items from database
  class Statement_Result 
  { 
  private $_bindVarsArray = array(); 
  private $_results = array(); 
  
  public function __construct(&amp;$stmt) 
  { 
      $meta = $stmt-&gt;result_metadata(); 
  
      while ($columnName = $meta-&gt;fetch_field()) 
          $this-&gt;_bindVarsArray[] = &amp;$this-&gt;_results[$columnName-&gt;name]; 
  
      call_user_func_array(array($stmt, 'bind_result'), $this-&gt;_bindVarsArray);
  
      $meta-&gt;close(); 
  } 
  
  public function Get_Array() 
  { 
      return $this-&gt;_results;    
  } 
  
  public function Get($column_name) 
  { 
      return $this-&gt;_results[$column_name]; 
  } 
  } 
  ?&gt;
  </code></pre>
  
  <p>And just as a test, I'm trying to pull all the news in my db by:</p>
  
  <pre><code>&lt;?php
  require_once('classes/mysqlicon.php');
  $testing = new MYSQLIDB;
  $testing-&gt;select('*','news',null,null,'orderByLimit','id','DESC',4);
  ?&gt;
  </code></pre>
  
  <p>But what I really want is stuff that can do the equivalent of this:</p>
  
  <pre><code>&lt;?php
      /*
      require('config/dbconfig.php');
      $query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
      if ($stmt = $mysqli-&gt;prepare($query)) {
          // execute statement
          $stmt-&gt;execute();
  
          // bind result variables
          $stmt-&gt;bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);
  
          // fetch values
          while ($stmt-&gt;fetch()) {*/
  
              //echo 'id: '. $id .' title: '. $title;
              echo "&lt;table border='0'&gt;";
              $shortDescLengthn = strlen($descn);
              if ($shortDescLengthn &gt; 106) {
                  $sDCutn = 106 - $shortDescLengthn;
                  $shortDescn = substr($descn, 0, $sDCutn);
              } else {
                  $shortDescn = $descn;
              }
              echo "&lt;h1&gt;$titlen&lt;/h1&gt;"; 
              echo "&lt;tr&gt;&lt;td&gt;$shortDescn...&lt;/td&gt;&lt;/tr&gt;"; 
              echo '&lt;tr&gt;&lt;td&gt;&lt;a href="javascript:void(0);" onclick="' 
              . 'readMore(' . $idn . ',' . htmlspecialchars(json_encode($titlen)) . ',' 
              . htmlspecialchars(json_encode($categoryn)) . ',' 
              . htmlspecialchars(json_encode($descn)) . ',' . htmlspecialchars(json_encode($postdaten)) . ',' 
              . htmlspecialchars(json_encode($authorn)) . ')"&gt;Read More&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;'; 
              echo "&lt;tr&gt;&lt;td&gt;Written by: $authorn&lt;/td&gt;&lt;/tr&gt;"; 
              echo '&lt;tr&gt;&lt;td&gt;&lt;img src="images/hardcore-games-newsbar-border.png" width="468px" /&gt;&lt;/td&gt;&lt;/tr&gt;'; 
          }
          echo "&lt;/table&gt;&lt;br /&gt;";
  
          /* close statement */
          $stmt-&gt;close();
      }
  
      /* close connection */
      $mysqli-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>Again, please, please explain in detail. I'm a blockhead sometimes.</p>