Forum Moderators: coopster

Message Too Old, No Replies

mysql_num_rows() help

         

electricocean

4:37 am on May 9, 2005 (gmt 0)

10+ Year Member



I am trying to make a login script and I need to connect to the db. I use msql_num_rows to check if the password and unsername are correct but it always gives me the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/export/www/hosting/dkicks/admin/new_files/ses/index.php on line 27

here's part of my code (line 27 and such around it):

$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password= $password' LIMIT 1";
$result = mysql_query($query);
$check = mysql_num_rows($result);

if ($check!=1) {
$error = "<font color=\"red\" size=\"+1\">Login failed.</font>";
print $error;
}

else if ($check == 1){
session_start();
$_SESSION['username'] = $username;

header("Location: admin.php");
}

any ideas whats wrong?

electricocean

grandpa

5:07 am on May 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$query = "SELECT * FROM users WHERE username='$username' AND password= $password' LIMIT 1";

You have omitted a single quote at $password. See if this corrects the error:
$query = "SELECT * FROM users WHERE username='$username' AND password= '$password' LIMIT 1";

Any time you see the error, supplied argument is not a valid MySQL result resource, look to your query first. I've only seen that error on malformed query statements.

electricocean

5:23 am on May 9, 2005 (gmt 0)

10+ Year Member



thank you grandapa. It works now.

Just for futur reference, what is the!=1 and == 1? I know that!= mean does not equal to and == means equals but what is the 1 after bothe of them mean?

grandpa

5:46 am on May 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



what is the 1 after bothe of them mean?
$check = mysql_num_rows($result);

1 is simply the value being tested in the variable $check. Ideally, the value of $check will be 1 and only 1, since only 1 row should be returned from the query. But... more than 1 row could be returned and the value of $check will indicate how many rows were actually returned. The code is validating that only one row was returned from the query before allowing any further action.

Stormfx

5:46 am on May 9, 2005 (gmt 0)

10+ Year Member



The 1 is the number of rows that have to be returned in order for the login to be successful. Or, in other words, there has to be a username + password match.

Stormfx

5:47 am on May 9, 2005 (gmt 0)

10+ Year Member



Grandpa's faster :/ Hehe