PHP“期望参数 1 为资源,给定布尔值”

发布于 2024-11-02 13:09:21 字数 958 浏览 2 评论 0原文

我已经修复了这个问题,clean 函数有问题,还有一些其他小错误,谢谢您的帮助。

我的 PHP 代码似乎有问题。我不断收到错误:

警告:mysql_fetch_array() 期望参数 1 为资源,给定布尔值 警告:mysql_num_rows() 期望参数 1 为资源,给定字符串

        public function __construct() {
            global $db, $core;

            $this->sessionID = $core->encrypt(session_id());

            $sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionId}'");
            $sessionRow = $db->assoc($sessionQuery);

            if($db->num($sessionQuery) > 0) {
                $userQ = $db->query("SELECT * FROM users WHERE id = '{$sessionRow['user_id']}'");
                $this->loggedIn = true;
                $this->userData = $db->assoc($userQ);
            }       
        }       
    }
    $user = new User();

?>  <br /><br />



I have fixed this, there was a problem in the clean function and a few other little errors, thankyou for all your help.

I seem to have a problem with my PHP Code. I keep getting the errors:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given
Warning: mysql_num_rows() expects parameter 1 to be resource, string given

        public function __construct() {
            global $db, $core;

            $this->sessionID = $core->encrypt(session_id());

            $sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionId}'");
            $sessionRow = $db->assoc($sessionQuery);

            if($db->num($sessionQuery) > 0) {
                $userQ = $db->query("SELECT * FROM users WHERE id = '{$sessionRow['user_id']}'");
                $this->loggedIn = true;
                $this->userData = $db->assoc($userQ);
            }       
        }       
    }
    $user = new User();

?>  <br /><br />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟─花易冷 2024-11-09 13:09:21

您将 $this->sessionId 而不是 $this->sessionID 传递给查询。请注意大小写的差异。

You're passing $this->sessionId instead of $this->sessionID to the query. Note the difference in capitalization.

心意如水 2024-11-09 13:09:21

$sessionQuery 不是有效的 mysql 资源,因为您在查询中传递了错误的变量,因此查询失败。

我建议处理 mysql 错误以避免此类警告。

您将会话 ID 存储在 $this->sessionID 中,并在查询中使用 $this->sessionId
应该是

$db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionID}'");

$sessionQuery is not a valid mysql resource because you are passing the wrong variable in query hence query got failed.

I would suggest to handle mysql error to avoid this type of warnings.

You are storing session id in $this->sessionID and using the $this->sessionId in query
it should be

$db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionID}'");
寻找我们的幸福 2024-11-09 13:09:21
public function __construct() {
            global $db, $core;

            $this->sessionID = $core->encrypt(session_id());

            $sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionID}'");
            if($sessionQuery){ // newly added line      
            $sessionRow = $db->assoc($sessionQuery);

            if($db->num($sessionQuery) > 0) {
                $userQ = $db->query("SELECT * FROM users WHERE id = '{$sessionRow['user_id']}'");
                $this->loggedIn = true;
                $this->userData = $db->assoc($userQ);
            } 
            } // newly added line      
        }       
    }
    $user = new User();
public function __construct() {
            global $db, $core;

            $this->sessionID = $core->encrypt(session_id());

            $sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionID}'");
            if($sessionQuery){ // newly added line      
            $sessionRow = $db->assoc($sessionQuery);

            if($db->num($sessionQuery) > 0) {
                $userQ = $db->query("SELECT * FROM users WHERE id = '{$sessionRow['user_id']}'");
                $this->loggedIn = true;
                $this->userData = $db->assoc($userQ);
            } 
            } // newly added line      
        }       
    }
    $user = new User();
梦断已成空 2024-11-09 13:09:21

当查询中存在语法错误时,就会发生这种情况。请通过在 PhpMyAdmin 中运行查询来确认您的查询没有语法错误。

另外,您还应该确保 PHP 设置为报告警告
http://php.net/manual/en/function.error-reporting.php

您可以使用 mysql_error 以编程方式检查连接是否有错误
http://php.net/manual/en/function.mysql-error.php

创建连接后以及运行每个查询后检查是否有错误。如果查询中存在语法错误,则资源对象仅是 false,并且可以使用 mysql_error 轻松识别

This happens when there is a syntax error in your query. Please confirm that your query has no syntax errors by running the query in PhpMyAdmin.

Also you should make sure that PHP is set to report warnings
http://php.net/manual/en/function.error-reporting.php

You can check your connection for errors programmatically using mysql_error
http://php.net/manual/en/function.mysql-error.php

Check for errors after you've created your connection and after every query you run. The resource object is only false if there was a syntax error in your query, and it is easily identifiable by using mysql_error

北方的巷 2024-11-09 13:09:21

您将整个 SQL 字符串传递给 clean 函数中的 mysql_real_escape_string (由于某种原因,您从问题中删除了该函数)。

这会导致传递的单引号被转义,从而导致格式错误的 SQL 字符串,从而导致返回 FALSE,而不是有效的资源。

$sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionId}'");

public function query($string) {
    global $core;
    $string = $core->clean($string);
    return mysql_query($string);
}

public function clean($string, $fordb = true) {
    if(is_array($string)) {
        foreach($string as $key => $value) {
            $string[$key] = $this->clean($value, $fordb);
        }

        return $string;
    } else {

        $string = trim($string);
        $input = htmlentities($input, ENT_COMPAT);

        if($fordb == true) {
            $string = mysql_real_escape_string($string);
            return $string;
        }
    }
}

You are passing the entire SQL string to mysql_real_escape_string in your clean function (which for some reason you deleted from your question).

This causes the passed single quotes to be escaped which results in a malformed SQL string which results in FALSE being returned, instead of a valid resource.

$sessionQuery = $db->query("SELECT * FROM sessions WHERE `session_id` = '{$this->sessionId}'");

public function query($string) {
    global $core;
    $string = $core->clean($string);
    return mysql_query($string);
}

public function clean($string, $fordb = true) {
    if(is_array($string)) {
        foreach($string as $key => $value) {
            $string[$key] = $this->clean($value, $fordb);
        }

        return $string;
    } else {

        $string = trim($string);
        $input = htmlentities($input, ENT_COMPAT);

        if($fordb == true) {
            $string = mysql_real_escape_string($string);
            return $string;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文