Reputation: 13
I'm trying to create a script that i can keep referring to which would display database entries. I believe my problem is when I'm trying to display the result I have no idea on how to call for mysqli and query. the ERROR I'm getting is Call to a member function fetch_row() on a non-object in
class connectDatabase {
public $category;
public $query;
public $mysqli;
public function __construct($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME)
{
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME);
return $this->mysqli = $mysqli;
}
public function queryInformation($category) {
$query = "SELECT * FROM postad WHERE `category` = $this->category ORDER by date desc";
return $this->query = $query;
}
public function displayResult() {
$mysqli = $this->mysqli ;
$query = $this->query;
$result = $mysqli->query($query);
while ($row = $result->fetch_row()) {
echo "$row[1] $row[2] $row[3] $row[4] </br>" ;
}
}
}
Upvotes: 0
Views: 312
Reputation: 3534
if you var_dump $result it's probably boolean false because the query is failing. It should be an object if it was successful. You need to have a fail safe in case the return value is boolean.
Check out the return values: http://php.net/manual/en/mysqli.query.php
Upvotes: 1
Reputation: 451
We can simplify this a bit.
Try this
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME);
$category = "something";
$result = $mysqli->query("SELECT * FROM postad WHERE `category` = '$category' ORDER by date desc");
while ($row = $result->fetch_row()) {
echo "$row[1] $row[2] $row[3] $row[4] </br>" ;
}
Upvotes: 0