PHP登录验证

PHP登录验证,php,Php,这些常量只包含数据库连接的详细信息 require_once 'includes/constants.php'; class mysql{ private $conn; function __construct(){ $this->conn = $conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die ('There was an error in the

这些常量只包含数据库连接的详细信息
require_once 'includes/constants.php';
class mysql{
    private $conn;

    function __construct(){
        $this->conn = $conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)
            or die ('There was an error in the connection');
        }

    function verify ($un, $pwd){


             $query = "SELECT * FROM User WHERE username = ? AND pass = ? LIMIT 1"; 

             if ($stmt = $this->conn->prepare($query))
             {
                 $stmt->bind_param('ss', $un, $pwd);
                 $stmt->execute();

                 if ($stmt->fetch());{
                     $stmt->close();
                     return true;
                     }
             }
        }

    }

?>
un和pwd都是从登录页面传递过来的,它们是从用户输入中提取出来的,我遇到的问题是,无论用户输入什么,用户总是登录的,它似乎无法根据数据库正确验证

if ($stmt->fetch());{
   $stmt->close();
   return true;
}

这个if后面是一个空语句,后面是一个新的{…}语句块。if仅与空语句关联,因此始终执行以下块

举例说明:

if (false)
    /* do nothing */;

{
    echo 'always goes here';
}

去掉多余的

如何调用验证方法?为什么它不返回false?还有,;内部if语句后面似乎放错了位置。if语句后面的分号有效吗?如果$stmt->fetch;实际的数据收集最好不在OOP范围内,相反,您应该研究MySQLI包装器以使查询更容易。调用包装器查询并验证结果@user2071225如果您正在格式化代码,请至少保留错误。对不起,我不是有意这么做的!
if ($stmt->fetch());{
                   ^------- THIS!
    $stmt->close();
    return true;
}
if (false)
    /* do nothing */;

{
    echo 'always goes here';
}