php login if statement error

Hi, I don’t have that much experience with php coding yet and for that reason cannot find the mistake in if statement that is hindering me to be able to login. Here is the code:

<?php if(empty($_POST)) { $status = 'Type your username and password below to log in.'; } else { $user = $_POST['user']; $passwd = $_POST['passwd']; } $error_list = array(); if(empty($user)) { $error_list[] = 'Please enter your username.'; } if(empty($passwd)) { $error_list[] ='Please enter your password'; } if(empty($error_list)) { require('../../sql_connect.php'); $sql = "SELECT id "; $sql .= "FROM members "; $sql .= "WHERE user='$user' "; $sql .= "AND pass='$passwd' "; $result = mysql_query($sql); if(mysql_num_rows($result) == 1) { session_start(); $_SESSION['logged_in'] = true; header('Location: index.php?mnu=home'); } else { $status = 'The username and/or password you supplied was incorrect.'; } } else { $status = '
    '; foreach($error_list as $error_message) { $status .= "
  • $error_message
  • "; } $status .= '
'; } ?>

The if statement that holds the code for the “== 1” is not functioning correct. In fact the error that I am getting is:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/investp8/public_html/admin/login_head.php on line 26

Can anyone help me?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/investp8/public_html/admin/login_head.php on line 26

This means that $result is not a valid MySQL result resource. So what’s coming back from your mysql_query($sql) is not a result. You should do error checking on your database queries.

$result = mysql_query($sql); if (!$result) { die('Invalid query: ' . mysql_error()); }

http://php.net/ is a good resource for things like this.

The error that is printed should give you a better idea as to why this is not working how you are expecting it to.

ok, thanks. I’ll look into php.net as well.

Sponsor our Newsletter | Privacy Policy | Terms of Service