简体   繁体   中英

PHP if Statement mysqli_num_rows not working

I have a code which asks if a row exists in a database and if it does, it will echo a message. However, nothing is echoed no matter what input I put in.

Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
</head>
<body>

  <?php

    $mysqlhost="host";
    $mysqldatabase="b33_15887129_Accounts";
    $mysqlusername="username";
    $mysqlpassword="password";
    $connect=new mysqli($mysqlhost,$mysqlusername,$mysqlpassword);

    if (!$connect) {
      die("Connection failed: " . $connect->connect_error);
    }

    echo "Connected Successfully";

    mysqli_select_db($connect,"b33_15887129_Accounts");
    $loginusername=$_POST['loginusername'];
    $loginpassword=$_POST['loginpassword'];
    $checkifexist="SELECT * FROM Users WHERE Username='$loginusername' AND 'Password=$loginpassword'";
    $runquery=mysqli_query($connect,$checkifexist);
    $numofrows=mysqli_num_rows($runquery);

    if($numofrows==1){
      echo "Successfully logged in!";
    }else{
      echo "Failed to log in";
    }

  ?>
</body>
</html>

It manages to echo Connected successfully but Successfully logged in and Failed to log in does not appear no matter what the input. Any help?

Your "failed to log in" isn't displaying because you've forgotten the echo.

if($numofrows==1){
    echo "Successfully logged in!";
}
else{
    echo "Failed to log in";

}

In order to get the sql to work, @Ranjith is correct, you'll need to alter the quote placement in your sql statement.

1) Your else statement have no echo !!!

2) Your SQL Statement is wrong! Check the quotes at the password! --> Password='$loginpassword'

In fact you should have a look at http://php.net/manual/en/mysqli.real-escape-string.php .

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