1

I am trying to make a db connection and check a table for existing data. However I recieve this error and I am unable to find the cause:


Warning: mysqli::query(): Couldn't fetch mysqli in /usr/share/nginx/www/me/container/class_lib.php on line 33

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /usr/share/nginx/www/me/container/class_lib.php on line 97

class dbHandler {

    static $dbObj          = null;
    protected $db_host     = 'localhost';       #db host
    protected $db_username = 'user';  #db username
    protected $db_password = 'password';        #db password
    protected $db_name     = 'db';  #db name

    function __construct()
    {
        # connect if not connected
        if(self::$dbObj === null)
        {
            self::$dbObj = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name)
            or die($this->dbObj->error);
        }

        mysqli_set_charset(self::$dbObj, "utf8");

    }

    // query: query the db
    public function query($query)
    {
        return self::$dbObj->query($query);
    }

}

/*
    class userLogin
    create user login
*/
class userLogin {

    private $username;
    private $password;

    function __construct($username, $password) {

        $this->_dbConn = new dbHandler();

        $this->username = $username;
        $this->password = $password;

    }

    public function verifyCredentials() {

        if($this->verifyUsername())
        {

        } else {

            exit;

        }

        if($this->verifyPassword())
        {

        } else {

            exit;

        }


    }

    private function verifyUsername() {

        if(!(preg_match('/[^a-z_\-0-9]/i', $this->username)))
        {

            return true;

        }

    }

    private function verifyPassword() {

        $query  = "SELECT * FROM tbl_user";
        $result = $this->_dbConn->query($query);
        $row    = mysql_fetch_assoc($result);

        var_dump($row);
    }

}

What am I doing wrong here?

anonamas
  • 243
  • 1
  • 6
  • 15
  • also, any dump or print of $row returns NULL even though there is data in the tbl_user table. – anonamas Jan 14 '14 at 14:39
  • Where do you create class instance? – Jenson M John Jan 14 '14 at 14:39
  • I call the class outside of this class_lib by using $username = $_POST['username']; $password = $_POST['password']; $login = new userLogin($username, $password); $login->verifyCredentials(); Then I call the db handler inside the userLogin constructor. – anonamas Jan 14 '14 at 14:40
  • 1
    `mysql_fetch_assoc($result)` should be `mysqli` in verifyPassword function. – Styphon Jan 14 '14 at 14:41
  • Still returns same errors and NULL. – anonamas Jan 14 '14 at 14:42
  • @Martin you can't just change it to the word mysqli, the rest of your code is Object Orientated. You need to make that OO as well. IE `$row = $result->fetch_assoc();` – Styphon Jan 14 '14 at 14:44
  • Fatal error: Call to a member function fetch_assoc() on a non-object in /usr/share/nginx/www/me/container/class_lib.php on line 97
    – anonamas Jan 14 '14 at 14:45
  • return self::$dbObj->query($query); – anonamas Jan 14 '14 at 14:47
  • possible duplicate of [mysqli::query(): Couldn't fetch mysqli](http://stackoverflow.com/questions/19937880/mysqliquery-couldnt-fetch-mysqli) – T.Todua May 08 '15 at 12:23

4 Answers4

4

All your wrapper is over a mysqli object oriented way, and suddenly you have this line?

$row    = mysql_fetch_assoc($result);

You have to use the fetch_assoc from the mysqli result object

$result->fetch_assoc()
Royal Bg
  • 6,988
  • 1
  • 18
  • 24
0

Your SQL query is clearly failing. Why you have a function query to call a query I don't know but I have a feeling that is the route cause of your issue. You would be better off with something like this:

class dbHandler extends mysqli {

    protected $db_host     = 'localhost';       #db host
    protected $db_username = 'user';  #db username
    protected $db_password = 'password';        #db password
    protected $db_name     = 'db';  #db name

    function __construct()
    {
            parent::__construct($this->db_host, $this->db_username, $this->db_password, $this->db_name);
            if($this->connect_errno)
            {
                    trigger_error('Unable to connect to the database [' . $this->connect_errno . ']: ' . $this->connect_error, E_USER_ERROR);
            }

    }

    public function __destruct()
    {
        parent::close();
    }

}

/*
    class userLogin
    create user login
*/
class userLogin {

    private $username;
    private $password;

    function __construct($username, $password) {

        $this->_dbConn = new dbHandler();

        $this->username = $username;
        $this->password = $password;

    }

    public function verifyCredentials() {

        if($this->verifyUsername())
        {

        } else {

            exit;

        }

        if($this->verifyPassword())
        {

        } else {

            exit;

        }


    }

    private function verifyUsername() {

        if(!(preg_match('/[^a-z_\-0-9]/i', $this->username)))
        {

            return true;

        }

    }

    private function verifyPassword() {

        $query  = "SELECT * FROM tbl_user";
        $result = $this->_dbConn->query($query);
            if($this->_dbConn->errno)
            {
                trigger_error('Error fetching users from table. Query: ' . $query . '. Error: ' . $this->_dbConn->error);
                return false;
            }
            if($result->num_rows)
            {
                while($row = $result->fetch_assoc())
                {
                    var_dump($row);
                }
            }
    }

}

Let me know how you get on with that.

Styphon
  • 10,304
  • 9
  • 52
  • 86
0

So... I never gave the mysql user from localhost any grants. Only from the remote LAN. Problemo solved.

anonamas
  • 243
  • 1
  • 6
  • 15
0

Probably you are closing the connection too early? DBConnect->close();?

so, if you try to execute any query after that, you will get an error!

T.Todua
  • 53,146
  • 19
  • 236
  • 237