PHP“期望参数 1 为资源,给定布尔值”
我已经修复了这个问题,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将
$this->sessionId
而不是$this->sessionID
传递给查询。请注意大小写的差异。You're passing
$this->sessionId
instead of$this->sessionID
to the query. Note the difference in capitalization.$sessionQuery
不是有效的 mysql 资源,因为您在查询中传递了错误的变量,因此查询失败。我建议处理 mysql 错误以避免此类警告。
您将会话 ID 存储在
$this->sessionID
中,并在查询中使用$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 queryit should be
当查询中存在语法错误时,就会发生这种情况。请通过在 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 usingmysql_error
您将整个 SQL 字符串传递给
clean
函数中的mysql_real_escape_string
(由于某种原因,您从问题中删除了该函数)。这会导致传递的单引号被转义,从而导致格式错误的 SQL 字符串,从而导致返回 FALSE,而不是有效的资源。
You are passing the entire SQL string to
mysql_real_escape_string
in yourclean
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.