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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题