$check="SELECT * FROM users WHERE name = '$_POST[name]'";
$rs = mysqli_query($conn,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
echo "User Already in Exists<br/>";
}else
echo "User does Not exist";
这是正确的方法吗?它说用户不管怎样都不存在;我知道有一个名为&#34; test&#34;在数据库中。
答案 0 :(得分:1)
// this line makes mysqli throw exceptions so you'll never have to check
// return values for false
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// prepare a statement checking for existence
$stmt = $conn->prepare('SELECT 1 FROM users WHERE name = ?');
// bind the POST param to the parameter
$stmt->bind_param('s', $_POST['name']);
$stmt->execute();
// if there are any rows, that means the name exists
if ($stmt->fetch()) {
echo 'User "', htmlspecialchars($_POST['name']), '" already exists<br/>';
} else {
echo 'User "', htmlspecialchars($_POST['name']), '" does not exist<br/>';
}