mysql_query() 期望参数 2 是资源,给定的布尔值 [英] mysql_query() expects parameter 2 to be resource, boolean given

查看:37
本文介绍了mysql_query() 期望参数 2 是资源,给定的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在我的网站上注册时,我都会收到这些错误,它不会将表保存在 cms_users 下,因此我无法登录.

[14-Aug-2014 19:58:53 UTC] PHP 警告:mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: 无法在第 378 行的/home/blabbers/public_html/includes/classes.php 中的SERVER IP"(4) 上连接到 MySQL 服务器[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_select_db() 期望参数 2 是资源,布尔值在第 379 行的/home/blabbers/public_html/includes/classes.php 中给出[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_query() 期望参数 2 为资源,布尔值在第 396 行的/home/blabbers/public_html/includes/classes.php 中给出[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_result() 期望参数 1 为资源,在第 420 行的/home/blabbers/public_html/includes/classes.php 中给出空值[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_query() 期望参数 2 为资源,布尔值在第 396 行的/home/blabbers/public_html/includes/classes.php 中给出[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_fetch_assoc() 期望参数 1 为资源,在第 400 行的/home/blabbers/public_html/includes/classes.php 中给出空值[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_query() 期望参数 2 为资源,布尔值在第 396 行的/home/blabbers/public_html/includes/classes.php 中给出[2014 年 8 月 14 日 19:58:53 UTC] PHP 警告:mysql_fetch_assoc() 期望参数 1 为资源,在第 400 行的/home/blabbers/public_html/includes/classes.php 中给出空值

这是文件.马里奥告诉我这还不够,希望这行得通.

class HoloDatabase {var $connection;变量 $ 错误;var $lastquery;函数 HoloDatabase($conn){开关($conn['服务器']){案例mysql":$this->connection = mysql_connect($conn['host'].":".$conn['port'], $conn['username'], $conn['password'], true);mysql_select_db($conn['database'],$this->connection) 或 $this->error = mysql_error();休息;案例pgsql":$this->connection = pg_connect("host=".$conn['host']." port=".$conn['port']." dbname=".$conn['database']." user=".$conn['username']." password=".$conn['password']);休息;案例sqlite":$this->connection = sqlite_open($conn['host'], 0666, $this->error);休息;案例mssql":$this->connection = mssql_connect($conn['host'].",".$conn['port'],$conn['username'],$conn['password'],true);休息;}}}类 mysql 扩展 HoloDatabase {功能查询($查询){if(defined('DEBUG')){ $this->lastquery = $query;}$query = mysql_query($query,$this->connection);返回 $query;}函数 fetch_assoc($query){$result = mysql_fetch_assoc($query);if(defined('DEBUG')){ $error = mysql_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 fetch_row($query){$result = mysql_fetch_row($query);if(defined('DEBUG')){ $error = mysql_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 fetch_array($result,$result_type=0){$result = mysql_fetch_array($result,$result_type);if(defined('DEBUG')){ $error = mysql_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 num_rows($query){$result = mysql_num_rows($query);if(defined('DEBUG')){ $error = mysql_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数结果($query,$row=0,$column=0){$result = mysql_result($query,$row,$column);if(defined('DEBUG')){ if($result == false){ echo mysql_error($this->connection) ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 insert_id($query=null){返回 mysql_insert_id($this->connection);}}类 pgsql 扩展了 HoloDatabase {功能查询($查询){if(defined('DEBUG')){ $this->lastquery = $query;}$query = pg_query($this->connection,$query);返回 $query;}函数 fetch_assoc($query){$result = pg_fetch_assoc($query);if(defined('DEBUG')){ $error = pg_last_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 fetch_row($query){$result = pg_fetch_row($query);if(defined('DEBUG')){ $error = pg_last_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 fetch_array($result,$result_type=0){$result = pg_fetch_array($result,null,$result_type);if(defined('DEBUG')){ $error = pg_last_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 num_rows($query){$result = pg_num_rows($query);if(defined('DEBUG')){ $error = pg_last_error($this->connection);if($result == false && !empty($error)){ echo $error ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数结果($query,$row=0,$column=0){$result = pg_fetch_result($query,$row,$column);if(defined('DEBUG')){ if($result == false){ echo pg_last_error($this->connection) ."<br/>查询出错:".$this->lastquery;} }返回 $result;}函数 insert_id($query){返回 pg_last_oid($query);}}

我该如何解决这个问题?

谢谢,马修

解决方案

MySQL 驱动程序需要连接资源;但是,当连接失败时,它返回 false.

因此出现错误预期资源,给出布尔值"

在这种情况下,您没有获得连接,因为 mysql 的参数没有正确配置.

你应该在某个地方 $db = new HoloDatabase($someConfiguration) - 看看 $someConfiguration 并确保你已经设置了服务器 IP/用户名 &正确设置密码.

作为替代,请确保 MySQL 通过 telnet xx.xx.xx.xx 3306 接受连接.如果是远程服务器,请确保将 mysql 设置为进行远程连接.

启用远程 MySQL 连接:ERROR 1045 (28000): Access denied for user>

I am getting these errors whenever I sign up on my website, It doesn't save the table under cms_users so I can't login.

[14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to MySQL server on 'SERVER IP' (4) in /home/blabbers/public_html/includes/classes.php on line 378
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_select_db() expects parameter 2 to be resource, boolean given in /home/blabbers/public_html/includes/classes.php on line 379
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_query() expects parameter 2 to be resource, boolean given in /home/blabbers/public_html/includes/classes.php on line 396
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_result() expects parameter 1 to be resource, null given in /home/blabbers/public_html/includes/classes.php on line 420
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_query() expects parameter 2 to be resource, boolean given in /home/blabbers/public_html/includes/classes.php on line 396
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/blabbers/public_html/includes/classes.php on line 400
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_query() expects parameter 2 to be resource, boolean given in /home/blabbers/public_html/includes/classes.php on line 396
    [14-Aug-2014 19:58:53 UTC] PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/blabbers/public_html/includes/classes.php on line 400

[EDIT] Here is the file. Mario told me it wasn't enough, Hopefully this work.

class HoloDatabase {
    var $connection;
    var $error;
    var $lastquery;
    function HoloDatabase($conn){
        switch($conn['server']){
            case "mysql":
                $this->connection = mysql_connect($conn['host'].":".$conn['port'], $conn['username'], $conn['password'], true);
                mysql_select_db($conn['database'],$this->connection) or $this->error = mysql_error();
                break;
            case "pgsql":
                $this->connection = pg_connect("host=".$conn['host']." port=".$conn['port']." dbname=".$conn['database']." user=".$conn['username']." password=".$conn['password']);
                break;
            case "sqlite":
                $this->connection = sqlite_open($conn['host'], 0666, $this->error);
                break;
            case "mssql":
                $this->connection = mssql_connect($conn['host'].",".$conn['port'],$conn['username'],$conn['password'],true);
                break;
        }
    }
}
class mysql extends HoloDatabase {
    function query($query){
        if(defined('DEBUG')){ $this->lastquery = $query; }
        $query = mysql_query($query,$this->connection);
        return $query;
    }
    function fetch_assoc($query){
        $result = mysql_fetch_assoc($query);
        if(defined('DEBUG')){ $error = mysql_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function fetch_row($query){
        $result = mysql_fetch_row($query);
        if(defined('DEBUG')){ $error = mysql_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function fetch_array($result,$result_type=0){
        $result = mysql_fetch_array($result,$result_type);
        if(defined('DEBUG')){ $error = mysql_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function num_rows($query){
        $result = mysql_num_rows($query);
        if(defined('DEBUG')){ $error = mysql_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function result($query,$row=0,$column=0){
        $result = mysql_result($query,$row,$column);
        if(defined('DEBUG')){ if($result == false){ echo mysql_error($this->connection) . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function insert_id($query=null){
        return mysql_insert_id($this->connection);
    }
}
class pgsql extends HoloDatabase {
    function query($query){
        if(defined('DEBUG')){ $this->lastquery = $query; }
        $query = pg_query($this->connection,$query);
        return $query;
    }
    function fetch_assoc($query){
        $result = pg_fetch_assoc($query);
        if(defined('DEBUG')){ $error = pg_last_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function fetch_row($query){
        $result = pg_fetch_row($query);
        if(defined('DEBUG')){ $error = pg_last_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function fetch_array($result,$result_type=0){
        $result = pg_fetch_array($result,null,$result_type);
        if(defined('DEBUG')){ $error = pg_last_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function num_rows($query){
        $result = pg_num_rows($query);
        if(defined('DEBUG')){ $error = pg_last_error($this->connection); if($result == false && !empty($error)){ echo $error . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function result($query,$row=0,$column=0){
        $result = pg_fetch_result($query,$row,$column);
        if(defined('DEBUG')){ if($result == false){ echo pg_last_error($this->connection) . "<br />Query that errored: ".$this->lastquery; } }
        return $result;
    }
    function insert_id($query){
        return pg_last_oid($query);
    }
}

How do I fix this?

Thanks, Matthew

解决方案

The MySQL driver is expecting a connection resource; however when the connection fails it returns false.

Hence the error "expected resource, boolean given"

In this case, you're not getting a connection, because a parameter for mysql hasn't been configured correctly.

You should have somewhere $db = new HoloDatabase($someConfiguration) - take a look at $someConfiguration and make sure you've set the server IP / username & password up correctly.

In lieu of that, make sure that MySQL is accepting connections via telnet xx.xx.xx.xx 3306. If it's a remote server, make sure that mysql is set up to take remote connections.

Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user

这篇关于mysql_query() 期望参数 2 是资源,给定的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆