我知道已有一些关于它的帖子,但我得到了其他解决方案:
$sql= 'SELECT * FROM pr_users WHERE nick = :nick ';
$sqldot = $db->prepare($sql);
$checkif = $sqldot->execute(array(':nick' => $nick));
if (count($checkif) == 1) {
$_SESSION['ng'] = "<p class=\"text-warning\">Nickname already exists!</p>";
header("location: ../register.php");
} else {
$final++;
$_SESSION['nick'] = $nick;
}
$ db在这里定义:
$db = new PDO(
"mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
)
);
如果$ checkif中有任何字符,我会计算,但出于某种原因即使“name”不存在 - &gt; $checkif
仍然有一些字符,所以它告诉我它已经存在,这是错误的。
答案 0 :(得分:1)
execute function成功时返回TRUE,失败时返回FALSE。你的if条件if(count($ checkif)== 1)将永远为真。
您需要在执行()之后获取结果并检查结果的计数。
$checkif = $sqldot->execute(array(':nick' => $nick));
$result = $sqldot->fetch();
if (!empty($result)) {
$_SESSION['ng'] = "<p class=\"text-warning\">Nickname already exists!</p>";
header("location: ../register.php");
} else {
$final++;
$_SESSION['nick'] = $nick;
}