我试图检查用户名是否已在我的数据库中使用,但它没有回应任何内容(我运行时出现空白页),我想得到一个真或假的回复
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data1") or die(mysql_error());
$usercheck = "john";
$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$check = mysql_query("SELECT EXISTS(SELECT 1 FROM users WHERE username = $sanitizeduser)");
echo $check;
?>
答案 0 :(得分:3)
您已使用mysqli_query
和mysql_
函数
试试这个,
$link = mysqli_connect("localhost","root","","data1") or die("Error " . mysqli_error($link));
$usercheck = "john";
$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$result = $link->query("SELECT username FROM users WHERE username = '".$sanitizeduser."' ");
$row_cnt = $result->num_rows;
if($row_cnt>0){
echo "User Exists"; // echo 1;
}else{
echo "No User found"; //echo 0;
}
答案 1 :(得分:2)
你正在混合mysql和mysqli:
所以你的问题只是一个错字,只需更改函数名称:
<?php
$link = mysqli_connect("localhost", "root", "password","data1");
if (!$link){
// error handling goes here: mysqli_error()
}
$usercheck = "john";
$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$check = mysqli_query($link,"SELECT EXISTS(SELECT 1 FROM users WHERE username = $sanitizeduser) as check");
?>
同样要显示结果,您不能只使用echo $check
,例如,您必须使用mysqli_fecth_array
之类的内容。 See the manual
示例:
while($row = mysqli_fecth_array($check)) {
echo $row["check"] . "<br>";
}