1

I'm trying to make a log in system using PHP but when I purposely attempt to log in with incorrect credentials this error message appears:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ...

The message I want to echo when someone types in the wrong credentials appears, but it appears below the warning message. These are the lines of code associated with the line of code causing the warning message to appear:

$query = mysql_query("SELECT * FROM users WHERE username='$user'");
if ($numrows == 1){
$row = mysql_fetch_assoc($query);
$dbid = $row['id'];
$dbuser = $row['username'];
$dbpass = $row['password'];
$dbactive = $row['active'];

And here is the actual line of code itself:

$numrows = mysql_num_rows($query);

I may need to edit the question to add more lines of code, please tell me if I do. Also this question may be a possible duplicate of this post: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in but I'm not too sure though. But please tell me what could be causing this warning message to appear.

Community
  • 1
  • 1
Mel
  • 33
  • 1
  • 1
  • 4

1 Answers1

5

If mysql_query returns false, this is boolean, so you need to check it first before passing it on to mysql_num_rows

$query = mysql_query("SELECT * FROM users WHERE username='$user'");

if($query)
  $numrows = mysql_num_rows($query);
else
  die("something failed");
Jake N
  • 10,535
  • 11
  • 66
  • 112