0

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>" ;

    }

    }
}
3
  • Why are you referencing $this->category when you are supposedly passing it as a parameter? Commented Jan 28, 2013 at 4:14
  • I meant to put $category Commented Jan 28, 2013 at 4:16
  • what is your problem exactly? i don't feel like you addressed the problem you're having. Commented Jan 28, 2013 at 4:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>" ;

    }

4 Comments

that's the same thing without the re usability of oop.
@MarshallHouse: Which isn't a bad thing for a code example to find the error.
I'm trying to implement OOP. If I wasn't it'd be a lot easier. The error I'm gettting is Fatal error: Call to a member function fetch_row() on a non-object in The error should be in while ($row = $result->fetch_row()) or $result = $mysqli->query($query);
@AnthonyLiriano Gotya, didn't realize OOP was the goal. I was just trying to simplify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.