php mysql_num_rows() 期望参数 1 是资源,字符串在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18757341/
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
mysql_num_rows() expects parameter 1 to be resource, string given in
提问by Benyaman
I have read through many other threads about this exact problem, but i for some reason can not solve my problem. Really need some help.
我已经阅读了许多关于这个确切问题的其他线程,但由于某种原因我无法解决我的问题。真的需要一些帮助。
if (!$username||!$password||!$email)
echo "Please fill out all fields";
else
{
//encrypt password
$password = md5($password);
//check if username already taken
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
if (mysql_num_rows($check)>=1)
echo "Username already taken";
else
It said
它说
Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /Users.....
警告:mysql_num_rows() 期望参数 1 是资源,/Users 中给出的字符串.....
if (mysql_num_rows($check)>=1)
This line..but when i run it in phpmyadmin, it returns results to me ok.
if (mysql_num_rows($check)>=1)
这一行..但是当我在 phpmyadmin 中运行它时,它会向我返回结果。
Please help
请帮忙
回答by Jite
Change:
改变:
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
to
到
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'") or die(mysql_error());
And you will see any potential errors that happens in the query.
您将看到查询中发生的任何潜在错误。
Also, I would highly recomend using either PDO or Mysqli instead of mysql functions, as they are deprecated and will be removed in future php versions.
此外,我强烈建议使用 PDO 或 Mysqli 而不是 mysql 函数,因为它们已被弃用,并将在未来的 php 版本中删除。
回答by Guru
Try to like this:
尝试喜欢这个:
$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
回答by Shafeeque
First You make sure that connection established correctly to the database.
首先确保正确建立到数据库的连接。
Instead of writing query directly in
而不是直接在
$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
Store the query in variable as
将查询存储在变量中
$query = "SELECT * FROM Test WHERE username = '".$username."'";
and execute it as
并将其执行为
$check = mysql_query($query);
if you are still facing the issue,
如果您仍然面临这个问题,
echo the query as
将查询回显为
echo $query;
and execute the query directly in phpmyadmin and you can find the issue.
并直接在phpmyadmin中执行查询即可找到问题。
I hope this helps.
我希望这有帮助。
回答by sharif2008
You can try like this
你可以这样试试
$sql= "SELECT * FROM Test WHERE username = '".$username."'";
$check = mysql_query($sql);
I think your $checkreturns null that is no user with this username and null can't be a parameter mysql_num_rows()function.
我认为您的$check返回空值,该用户名不是用户,空值不能是参数mysql_num_rows()函数。
if($check)
{
echo "Username already taken";
}
else
{
echo "Username available";
// do other actions
}
回答by Ali Abdul
Ok, if anyone having the same issue, just add the variable within the if() statement two times, like so:
好的,如果有人遇到同样的问题,只需在 if() 语句中添加变量两次,如下所示:
$Query = mysql_query($sql);
IF($Query && mysql_num_rows($Query)> 0){ // continue with the code}
This should fix the issue.
这应该可以解决问题。