检查mysql用户是否存在 [英] Check if mysql user exists

查看:115
本文介绍了检查mysql用户是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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";

这是正确的方法吗?它说无论如何用户都不存在;我知道数据库中有一个名为test"的用户.

Is this the correct way? It says User does Not exist no matter what; and I know there is a user named "test" in the db.

推荐答案

// 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/>';
}

这篇关于检查mysql用户是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆