我有一个登录数据库,我有很多用户,当我尝试使用数据库中首先列出的用户登录时,它运行正常。但是,当我与另一个用户登录时,它不起作用,它似乎只会选择第一行。我的查询出了什么问题?
$username = $_POST['Username'];
$salt = substr($username, 0, 2);
$password = crypt($_POST['Password'], $salt);
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$query = $dbh->prepare("SELECT * FROM `7Ducklings` WHERE Username = ? AND Password = ?");
$array = array($username, $password);
$query->execute($array);
$numrows = $query->fetchColumn();
if($numrows == 1)
{
echo "success";
}
else
{
}
$dbh = null;
编辑:当打印$ numrows成功时,我得到1,否则我得到零。这可能是盐问题吗?
答案 0 :(得分:0)
修改if语句以检查Empty或TRUE / FALSE。 fetchColumn()返回结果集下一行的单个列,如果没有更多行,则返回FALSE。 PDO FetchColumn
$username = $_POST['Username'];
$salt = substr($username, 0, 2);
$password = crypt($_POST['Password'], $salt);
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$query = $dbh->prepare("SELECT * FROM `7Ducklings` WHERE Username = ? AND Password = ?");
$array = array($username, $password);
$query->execute($array);
$numrows = $query->fetchColumn();
if(!empty($numrows))
{
echo "success";
}
else
{
}
$dbh = null;