简体   繁体   中英

MySQL query acting weird

I've got:

mysql_connect($host,$username,$password);
@mysql_select_db("db") or die("Error: Cannot select database");
$query = "select password from users where name = '".$_POST['login-userid']."'";
$result = mysql_query($query);

if ($result == false) {
    echo "Invalid username or password";
} else {
    if (mysql_result($result,0) == hash('sha256', $_POST['login-password'])) {
         echo "Logging in...";
    }
}

For some reason I keep getting an error for the mysql_result line, even when it shouldn't be executed (when the username doesn't exist, ie $result evaluates to false).

mysql_query will only return false if there is an error. In this case there is no error, there are just 0 rows.

You need to use mysql_num_rows to get the number of rows returned.

You can var_dump($result) and see the value, if it's a resource (according to php ), then your query was successful, if not false is returned. It's a boolean false but your == should still be respected if indeed false was returned.

mysql_query($query);

returns the result set of your query. It does not return true or false.

You may use mysql_num_rows() to check if the result of the query exist such as:

if(mysql_num_rows($query)) {
   // exist
} else {
   // does not exist
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM