gpt4 book ai didi

php - 如何检查用户是否存在于MySQL数据库中?

转载 作者:行者123 更新时间:2023-11-30 21:22:37 29 4
gpt4 key购买 nike

如何判断一个用户是否已经存在于数据库中?

我有变量 $name = $user_profile['name']; 它成功返回用户名。

这是我检查用户是否已存在于数据库中的代码部分。

$user_profile = $facebook->api('/me');
$name = $user_profile['name'];
$mysqli = new mysqli("asd", "asdf", "pw", "asdssst");
/* Create the prepared statement */
$stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM myTable WHERE userName = ?") or die("Prepared Statement Error: %s\n" . $mysqli->error);
/* Bind results to variables */
$stmt->bind_param('s', $name);
/* Execute the prepared Statement */
$stmt->execute();
$data = $stmt->fetch();
if ($data['num'] > 0) {
echo "bad";
print "user already exists\n";
} else {
echo "good";
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
print "No user in database\n";
}
/* Close the statement */
$stmt->close();

它总是返回:good No user in database 无论用户是否在数据库中。这意味着 $data['num'] 总是 0

附言我知道我需要使用 FB 用户 ID 而不是用户名,但在这种情况下我需要这样做。

最佳答案

我会用这样的东西

$mysqli = new mysqli('',"","","");
$query = 'SELECT username FROM user WHERE username = ?';
$stmt = $mysqli->prepare($query);
$name = 'asdf';
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($user);
if($stmt->fetch())
echo 'Username '.$user. ' exists';
else
echo 'User doesnt exists';
$stmt->close();

关于php - 如何检查用户是否存在于MySQL数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553591/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com