I have created my own database class to simplify MySQLi prepared statements and I am trying to access this in another class.
I have extending the database class to my profile class and tried to run the select query within my construct and this error is returned:
Fatal error: Call to a member function prepare() on null in /home/matt500b/dev2.csgoberry.com/includes/database.class.php on line 80
profile class construct:
public function __construct($username) {
$params = parent::SELECT("SELECT users_info.*, users.username, users.lastOnline FROM users_info INNER JOIN users ON users.id = users_info.user_id WHERE users.username = ?", array('s', $username));
$this->fname = (isset($params[0]['fname']) ? $params[0]['fname'] : null);
/*More settings done here*/
}
However if I run the query first then send the data to the construct like so:
$profileData = $db->SELECT('SELECT users_info.*, users.username, users.lastOnline FROM users_info INNER JOIN users ON users.id = users_info.user_id WHERE users.username = ?', array('s', $pid));
$pclass = new user_profile($profileData);
with the construct being:
public function __construct($params){
$this->fname = (isset($params[0]['fname']) ? $params[0]['fname'] : null);
}
Everything works nice. Of course the database instance was called using $db = new database(HOST, USER, PASSWORD, DATABASE, DEBUG); where my database construct is:
public function __construct($db_host, $db_user, $db_pass, $db_name, $debug){
$this->link = mysqli_connect($db_host, $db_user, $db_pass);
mysqli_select_db($this->link, $db_name);
$this->debug = $debug;
}
The error relates to this line: if ($stmt = $this->link->prepare($sql)) { and suggests that $sql is null however the two queries are exactly the same except for their origins (parent::SELECT vs $db->SELECT).
Any suggestions to make this work?
Many thanks
$this->linkis null not$sql. I don;t see this line of code in your code examples in the question. You question is pretty hard to follow in that it is not clear what the whole call stack looks like.