0
function __construct(mysqli $db, $country = NULL, $sport = NULL) {
    $this->db = $db;
    $this->country = base64_decode($country);
    $this->sport = base64_decode($sport);
}

public function GetColor($colorcode) {
    $query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
    $result = $this->db->query($query);
    while ($row = $result->fetch_assoc()) { // Line 21
        echo $row['naam_nl'];
    }
    $result->close();
}

Gives me:

Fatal error: Call to a member function fetch_assoc() on a non-object in /home/cloud/public/td/teamdresser.class.php on line 21

So I tried:

    $result = $this->db->query($query);
    while ($row = $this->db->fetch_assoc($result)) {
        echo $row['naam_nl'];
    }

And then...

Fatal error: Call to undefined method mysqli::fetch_assoc() in /home/cloud/public/td/teamdresser.class.php on line 21

I'm doing something wrong.. Can someone point me in the right direction?

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
  • 1
    The point is just that your query returns something else then a mysqli_result object. Probably `false`. - Is the `color`field a numeric value? If it isn't you missed the quotes in the query – idmean Jan 14 '14 at 14:27
  • Do some error checking. Your query is failing. Check the return value of `$this->db->query($query)`, and if it's `false` check `$this->db->error` for the error message. –  Jan 14 '14 at 14:29
  • possible duplicate of [Fatal error: Call to undefined method mysqli\_result::fetch\_all()](http://stackoverflow.com/questions/11664536/fatal-error-call-to-undefined-method-mysqli-resultfetch-all) – Jon Jan 14 '14 at 14:42

2 Answers2

1

This part is wrong:

public function GetColor($colorcode) {
    $query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';

PHP won't interpolate $colorcode when using single quotes. Use double quotes intead:

$query = "SELECT naam_nl FROM colors WHERE code = $colorcode";

Comments:

  • Always check for the return value! That makes spotting errors much easier.

  • Why don't you use prepared statements?

ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • Edited it, but still same error code. It's the `fetch_assoc` and not the query itself. – Joran Den Houting Jan 14 '14 at 14:35
  • @JoranDenHouting It *is* the query. Due to its fail, `fetch_assoc` will be "called on a non-object". Please include a `var_dump($result)` right after `$this->db->query`. – ComFreek Jan 14 '14 at 14:36
1

Like the comments say, you need to do some error checking:

public function GetColor($colorcode) {
    $query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
    $result = $this->db->query($query);
    if ($result === false) {
        // Throw or handle an error here
    } else {
        while ($row = $result->fetch_assoc()) { // Line 21
            echo $row['naam_nl'];
        }
        $result->close();
    }
}

Additionally, you need the mysqlnd drivers: Fatal error: Call to undefined method mysqli_result::fetch_all()

Community
  • 1
  • 1
Jon
  • 12,684
  • 4
  • 31
  • 44
  • I know, but the query isn't failing.. It's the `fetch_assoc`.. Still have the following error using your script: `Fatal error: Call to undefined method mysqli::fetch_assoc() in /home/cloud/public/td/teamdresser.class.php on line 21` – Joran Den Houting Jan 14 '14 at 14:34
  • This might be the solution: http://stackoverflow.com/questions/11664536/fatal-error-call-to-undefined-method-mysqli-resultfetch-all – Jon Jan 14 '14 at 14:41