I'm learning MySQLi in php and I can't understand why I am getting an Call to a member function prepare() on a non-object error.
Here's my code as below:
public function fetch_posts($num = 5)
{
    global $mysqli;
    if($stmt = $mysqli->prepare("SELECT * FROM posts ORDER BY id desc LIMIT ?")) 
    {
       /* Bind parameters
          s - string, b - boolean, i - int, etc */
       $stmt -> bind_param("i", $num);
       /* Execute it */
       $stmt -> execute();
       /* Bind results */
       $stmt -> bind_result($result);
       /* Fetch the value */
       $stmt -> fetch();
       /* Close statement */
       $stmt -> close();
    }        
    $mysqli -> close();
}
$mysqli is the MySQLi connection call, $mysqli = new mysqli(DB_HOST, DB_ID, DB_PW, DB); which resides in the __construct() function, both which are within the same class.
Have I done something obviously wrong here? I can't see it.
 
     
    