无法获取mysqli

时间:2021-03-15 20:11:44

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:

我正在尝试创建一个db连接,并检查一个现有数据的表。然而,我收到这个错误,我找不到原因:


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

警告:mysqli::query():无法在/usr/share/nginx/www/me/container/class_lib中获取mysqli第33行警告:mysql_fetch_assoc()期望参数1为资源,在/usr/share/nginx/www/me/container/class_lib中给出null。php在第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?

我在这里做错了什么?

4 个解决方案

#1


4  

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

你所有的包装是mysqli面向对象方式,突然有这条线吗?

$row    = mysql_fetch_assoc($result);

You have to use the fetch_assoc from the mysqli result object

您必须使用mysqli结果对象的fetch_assoc

$result->fetch_assoc()

#2


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:

您的SQL查询显然失败了。为什么你有一个函数查询来调用一个查询,我不知道,但是我有一种感觉,那就是你的问题的起因。你最好这样做:

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.

让我知道你是怎么做的。

#3


0  

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

所以…我从未向localhost的mysql用户授予任何授权。只从远程局域网。Problemo解决。

#4


0  

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

可能你关闭连接太早了?DBConnect - > close();?

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

因此,如果您试图在此之后执行任何查询,您将会得到一个错误!

#1


4  

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

你所有的包装是mysqli面向对象方式,突然有这条线吗?

$row    = mysql_fetch_assoc($result);

You have to use the fetch_assoc from the mysqli result object

您必须使用mysqli结果对象的fetch_assoc

$result->fetch_assoc()

#2


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:

您的SQL查询显然失败了。为什么你有一个函数查询来调用一个查询,我不知道,但是我有一种感觉,那就是你的问题的起因。你最好这样做:

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.

让我知道你是怎么做的。

#3


0  

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

所以…我从未向localhost的mysql用户授予任何授权。只从远程局域网。Problemo解决。

#4


0  

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

可能你关闭连接太早了?DBConnect - > close();?

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

因此,如果您试图在此之后执行任何查询,您将会得到一个错误!