0

PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/rute/dbcontroller.php on line 27

This error I don't know how can I fixed...I try with different methods here but without success... can you help me with this?

Here the code:

In index.php:

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/scripts/dbcontroller.php');
$db_handle = new DBController();

$sql = "SELECT * FROM PRODUCTS WHERE active != '0'";
$posts = $db_handle->runSelectQuery($sql);
?>

Here the dbcontroller.php // in line 27 is the PHP WARNING

<?php

require($_SERVER['DOCUMENT_ROOT'] . '/scripts/config.php');

class DBController {
    private $conn = "";
    private $host = DB_HOST;
    private $user = DB_USER;
    private $password = DB_PASS;
    private $database = DB_NAME;


    function __construct() {
        $conn = $this->connectDB();
        if(!empty($conn)) {
            $this->conn = $conn;            
        }
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }

    function runSelectQuery($query) {
        $result = mysqli_query($this->conn,$query);
        while($row=mysqli_fetch_assoc($result)) {  // HERE IS THE ERROR
            $resultset[] = $row;
        }
        if(!empty($resultset))
            return $resultset;
    }

    function executeInsert($query) {
        $result = mysqli_query($this->conn,$query);
        $insert_id = mysqli_insert_id($this->conn);
        return $insert_id;

    }
    function executeUpdate($query) {
        $result = mysqli_query($this->conn,$query);
        return $result;

    }

    function executeQuery($sql) {
        $result = mysqli_query($this->conn,$sql);
        return $result;

    }

    function numRows($query) {
        $result  = mysqli_query($this->conn,$query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;
    }
}
?>
asterix_jv
  • 824
  • 1
  • 14
  • 35
  • 1
    Test if the `$result` is a real object and not `false` before using it. mysqli_query _Returns FALSE on failure_ – JustOnUnderMillions Mar 28 '17 at 15:00
  • Just for your info: this class is just awful. – Your Common Sense Mar 28 '17 at 15:03
  • 1
    @JustOnUnderMillions with proper error reporting you don't have to test anything – Your Common Sense Mar 28 '17 at 15:12
  • @Your Common Sense What is wrong with the first sentence in my first comment? And with `Test` i mean do `if($result!==false)` or esle (conditiontest/check). How you then report or handle the error is not the real problem. But using a variable that was not validated (tested) is wrong i thing. But thnx, will careful use words in the future. Your first comment by the way: hits the nail – JustOnUnderMillions Mar 29 '17 at 09:13
  • @JustOnUnderMillions Nobody asks you to use a variable that was not validated(tested). You just don't understand how Exceptions work ;) s manual testing of *each(!)* query's result of yours is a terrible practice that makes your code awfully bloated. – Your Common Sense Mar 29 '17 at 09:16
  • @Your Common Sense __mysqli_query Returns FALSE on failure__ where is the error thrown? So thats fine to do for you: `mysqli_fetch_assoc(mysqli_query($this->conn,$query))`, sry dont get it. – JustOnUnderMillions Mar 29 '17 at 09:18
  • @JustOnUnderMillions `error reporting != exception handling` please be precise, and i argue in relevance to the level of the Q here. But whatever. To many ways to rome... have a nice. btw: do you have a link that shows the proper use of the mysqli_ extention? – JustOnUnderMillions Mar 29 '17 at 09:25
  • @JustOnUnderMillions not yet. I am in the process of writing it. But the top line there would be "The proper use of mysqli_extension is to use PDO instead." But still. You don't have to "handle" exceptions. They are errors all the same. Just tell mysqli to use exceptions and forget about testing every single query result. it's amazingly simple. – Your Common Sense Mar 29 '17 at 09:43

0 Answers0