I am trying to make my own CRUD class from scratch with PHP. Connection is fine, but when I try to execute query, it gives me null.
class CRUD {
private static $link; // Connection Var
private $error; // Display Mysql error
private $sql_query; // Contains SQL query stings
private $debug = TRUE; //TRUE will show SQL queries
public function __construct() {
$this->connect();
}
private function connect() {
try {
self::$link = new mysqli(HOST, USER, PASS, DB);
if (self::$link->error) {
throw new Exception('Error connecting to DB.');
}
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
public function query($sql) {
try {
if (self::$link->query($sql) === FALSE) {
throw new Exception("<p style='color:red;'> Query failed. </p> <br> ");
}
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
public static function fetch($res) {
try {
return self::$link->fetch_object($res);
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
}
$db = new CRUD();
$sql = "SELECT * from users";
$query = $db->query($sql);
And it returns null... Query is ok, i don't know what could be a problem. And when I try to use "fetch()" method it returns me error like this: Fatal error: Call to undefined method mysqli::fetch_object()
If someone can take a look and help please, I will apreciate. thanks