php “警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在...中给出”但我的查询是正确的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15723897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...' but my query is correct
提问by Caderade
Ok, so I know the question about 'why am I getting this warning with mysql_fetch_array...' has been asked several times, my problem is all the accepted answers state that the reasons the server is spitting this warning out is because the query itself is incorrect...this is not the case with me.
好的,所以我知道关于“为什么我用 mysql_fetch_array 收到此警告...”的问题已被多次询问,我的问题是所有已接受的答案都表明服务器发出此警告的原因是因为查询本身不正确……我不是这种情况。
Here is the code below:
这是下面的代码:
$dbhost = "host";
$dbuser = "user";
$dbpass = "pass";
$dbname= "db";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die ('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Failed.php?dberror=1">');
$token = mysql_escape_string($_GET['token']);
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
Everything within the 'while' statement is being executed just fine - it makes some changes to the DB which I can validate is happening. More importantly, the query never spits out any error details. I've tried testing for cases where $result===false and asking for error info but it won't return anything then either. From what I can tell, the query string is fine and is not failing.
'while' 语句中的所有内容都被很好地执行 - 它对我可以验证的数据库进行了一些更改。更重要的是,查询永远不会吐出任何错误细节。我已经尝试测试 $result===false 并询问错误信息的情况,但它也不会返回任何内容。据我所知,查询字符串很好并且没有失败。
What am I doing wrong here? Could there any other reason why PHP doesn't like my While parameters other than the SQL statement is bad (which again, I'm convinced it's not bad)?
我在这里做错了什么?PHP 不喜欢我的 While 参数而不是 SQL 语句的其他原因是否有其他原因(这又一次,我确信它不错)?
Also, I know I should be using mysqli/PDO....I plan to switch over to that in the near future, but I'm pulling my hair out just trying to make this work and I have no idea why it won't. It's more of a personal thing at this point...
另外,我知道我应该使用 mysqli/PDO ......我计划在不久的将来切换到那个,但我正在努力使这项工作顺利进行,我不知道为什么会这样'吨。在这一点上,这更像是个人的事情......
Thanks for your help, and let me know if you need any additional info. (PHP Version 5.3)
感谢您的帮助,如果您需要任何其他信息,请告诉我。(PHP 5.3 版)
Here is an echo of the query string ($query):
这是查询字符串 ($query) 的回显:
SELECT * FROM newuser WHERE token='6e194f2db1223015f404e92d35265a1b'
And here is a var_dump of the query results ($result): resource(3) of type (mysql result)
这是查询结果的 var_dump ($result): resource(3) of type (mysql result)
回答by Terje D.
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
If the die
statement is not executed, $result
is OK when you enter the while loop. The problem then is probably that you use $result
for a query inside the loop as well, eventually leading to it being set to false.
如果die
语句没有执行,$result
进入while循环就OK了。那么问题可能是您$result
在循环内也使用了查询,最终导致它被设置为 false。
回答by Last Breath
So for now i can say that the problem is not the mysql_escape_string
nether the using of mysql
at all neither access privilege from user name
and password
and what i want to tell you is to test the $result
and if it is a resource proceed with your while
block like this
所以现在我可以说,这个问题是不是mysql_escape_string
在使用的幽冥mysql
从所有没有访问权限user name
和password
,我想告诉你的是要测试什么$result
,如果它是一个资源与您进行while
块这样的
if(is_resource($result))
{
while($row = mysql_fetch_array($result))
{//process your code here}
}
and tell me if the code has been also executed :)
并告诉我代码是否也已执行:)
回答by Kermit
The query is notcorrect according to mysql_
. The error you're receiving is telling you that $result
is boolean (false).
根据 查询不正确mysql_
。您收到的错误告诉您这$result
是布尔值 (false)。
Where is $token
coming from? You best stop using mysql_
functions and use a prepared statement and a bound parameter.
哪里$token
来的?您最好停止使用mysql_
函数并使用准备好的语句和绑定参数。
回答by echo_Me
your escape is wrong try this
你的逃跑是错误的试试这个
$token = mysql_real_escape_string($_GET['token']);
instead of $token = mysql_escape_string($_GET['token']);
代替 $token = mysql_escape_string($_GET['token']);
This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
这个扩展从 PHP 5.5.0 开始被弃用,将来会被删除。
http://php.net/manual/en/function.mysql-real-escape-string.php
http://php.net/manual/en/function.mysql-real-escape-string.php