mysql_num_rows() expects parameter 1 to be resourc

2020-04-20 15:27发布

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.....

if (mysql_num_rows($check)>=1) This line..but when i run it in phpmyadmin, it returns results to me ok.

Please help

标签: php boolean
5条回答
叼着烟拽天下
2楼-- · 2020-04-20 16:05

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.

I hope this helps.

查看更多
对你真心纯属浪费
3楼-- · 2020-04-20 16:22

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.

查看更多
倾城 Initia
4楼-- · 2020-04-20 16:29

Try to like this:

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$result = mysql_query($query);  
$num_rows = mysql_num_rows($result); 
查看更多
beautiful°
5楼-- · 2020-04-20 16:29

Ok, if anyone having the same issue, just add the variable within the if() statement two times, like so:

$Query = mysql_query($sql); 
IF($Query && mysql_num_rows($Query)> 0){ // continue with the code}

This should fix the issue.

查看更多
家丑人穷心不美
6楼-- · 2020-04-20 16:30

You can try like this

$sql= "SELECT * FROM Test WHERE username = '".$username."'";
$check = mysql_query($sql);

I think your $check returns null that is no user with this username and null can't be a parameter mysql_num_rows() function.

if($check)
{
    echo "Username already taken";
}
else
{
  echo "Username available";
  // do other actions
}
查看更多
登录 后发表回答