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?

9
  • also, any dump or print of $row returns NULL even though there is data in the tbl_user table. Commented Jan 14, 2014 at 14:39
  • Where do you create class instance? Commented Jan 14, 2014 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. Commented Jan 14, 2014 at 14:40
  • 1
    mysql_fetch_assoc($result) should be mysqli in verifyPassword function. Commented Jan 14, 2014 at 14:41
  • Still returns same errors and NULL. Commented Jan 14, 2014 at 14:42

4 Answers 4

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I noticed. However this returns <b>Fatal error</b>: Call to a member function fetch_assoc() on a non-object in <b>/usr/share/nginx/www/me/container/class_lib.php</b> on line <b>97</b><br />
@Martin It seems like your query fails. Because query returns false. So try this: if ($result) { $result->fetch_assoc() }
Yes, it return false. How can this be? And no I do not use mysql anymore only mysqli.
I have removed it from the code. I now use $result->fetch_assoc();
It's ok, scratch my last comment. Coffee hasn't kicked in yet @Martin
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.

2 Comments

My code works when you use a root mysql login. How is that possible if my code is invalid?
@Martin I never said it was invalid, I said it was confusing and could be causing the issue.
0

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

Comments

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!

Comments

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.