doubiao7410 2015-11-19 19:45
浏览 84
已采纳

警告:mysqli_query()期望参数1为mysqli,boolean给出[duplicate]

Hi, i dont know how to solve this code...

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /var/www/dropcs.com/data/www/site.com/model.class.php on line 13

class Model 
{
    protected $_db;

    public function __construct()
    {
        if (!$this->_db) {
            $this->_db = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
        }

        $this->query("SET CHARSET utf-8;");
    }

    public function query($query)
    {
        return mysqli_query($this->_db, $query);
    }

    public function fetch($result, $mode = 'object')
    {
        if ($result) {
            switch($mode) {
                case 'row':
                    return mysqli_fetch_row($result);
                    break;

                case 'assoc':
                    return mysqli_fetch_assoc($result);
                    break;
            }
        }
    }
}

Can you help me please

</div>
  • 写回答

1条回答 默认 最新

  • duanmianhong4893 2015-11-19 22:15
    关注

    Your DB connection is failing. In your defence the manual mysqli_connect in this case is not completely clear. mysqli_connect() returns false if the connection fails.

    Try to diagnose the problem. Here's an example of how to write the error to the standard error log and exit.

    public function __construct()
    {
        if (!$this->_db) {
            $this->_db = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
        }
    
        if(!$this->_db) {
            error_log('Mysqli Connect failed: ' . mysqli_connect_errno() . ' : ' . mysqli_connect_error());
            exit();
        }
    
        $this->query("SET CHARSET utf-8;");
    }
    

    Along the same lines, write the values of `HOSTNAME, USERNAME, PASSWORD & DATABASE to somewhere to see any of these are incorrect.

    You've got some negative feedback because you don't appear to have tried diagnosing the issue in your question and the manual has good examples. If you're still stuck, show what you have tried to do to figure it out and you'll find SO a very helpful community.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?