I want to pass a mysqli object to a class. I create the object in one file:
$host = 'host';
$user = 'username';
$pw = 'pw';
$db = 'db';
$link = new mysqli($host,$user,$pw,$db);
which is included in one page:
include 'php/mysql.php';
include 'php/classes/Class.Dataconnector.php';
$dc = new Dataconnector($link);
and the class looks like this:
class Dataconnector {
protected $_link;
protected $_stub;
function __construct(mysqli $link) {
$_link = $link;
}
public function getPageContent($stub) {
$query = "select * from contents where pageId = (select id from pages where stub = '$stub')";
$result = mysqli_query($_link,$query);
return $result;
}
}
But I get this error:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in \php\classes\Class.Dataconnector.php on line 18
What is wrong ?
You need to use
$thiswhen accessing the class property$_linkin your class.In your constructor, you have:
This should be:
In your
getPageContent()method you have:This should be: