gpt4 book ai didi

php - 递归函数和mysqli关闭错误

转载 作者:行者123 更新时间:2023-11-29 00:06:00 25 4
gpt4 key购买 nike

我正在尝试为每个用户生成一个用户 ID 和关联的 6 位 token 。这些值对于活跃用户必须是唯一的。为了确保唯一性,我递归地使用相应的函数。

这些值是在页面加载时生成的。我的脚本应该可以正常工作,除了(大部分)当我快速重新加载页面时它会出错。除非我删除了 mysqli_close($dbLink) 语句。问题是我关闭了链接,还是递归使用了这些函数,这是一种组合,还是完全不同的东西?

错误:

Warning: mysqli_prepare(): Couldn't fetch mysqli in C:\xampp\htdocs\project\functions.php on line 68

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in C:\xampp\htdocs\project\functions.php on line 69

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in C:\xampp\htdocs\project\functions.php on line 70

Warning: mysqli_close(): Couldn't fetch mysqli in C:\xampp\htdocs\project\functions.php on line 71

代码:

$user_id = generateUserId();
$token = generateToken($user_id);

function generateUserId(){
global $dbLink;

$user_id = openssl_random_pseudo_bytes(128);
$user_id = hash('md5', $user_id);

$query = "SELECT * FROM users WHERE user_id = '{$user_id}' AND user_active = 1";
$result_set = mysqli_query($dbLink, $query);
if (mysqli_num_rows($result_set) != 0) {
generateUserId();
}

return $user_id;
}

function generateToken($user_id){
global $dbLink;

//generate Token with different numbers
$numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$token = array();

while (count($token) < 6) {
$number = array_rand($numbers);
if (!in_array($number, $token)) {
array_push($token, $number);
}
}

$token = implode($token);

//check if token exists
$query = "SELECT * FROM users WHERE user_token = '{$token}' AND user_active = 1";
$result_set = mysqli_query($dbLink, $query);
if (mysqli_num_rows($result_set) != 0) {
generateToken($user_id);
}

//save token and user id in database

$query = "INSERT INTO users ";
$query.= " (user_id, user_token)";
$query.= " VALUES (?, ?)";
$stmt = mysqli_prepare($dbLink, $query);
mysqli_stmt_bind_param($stmt, 'ss', $user_id, $token);
mysqli_stmt_execute($stmt);
mysqli_close($dbLink);

return $token;
}

最佳答案

问题在于关闭链接,你应该在函数调用后关闭它,而不是内部。

顺便说一句,如果您想在函数内部关闭它,请考虑在调用递归函数后不要执行任何类型的 sql 语句或函数。

我要做的是提取这段代码:

//save token and user id in database

$query = "INSERT INTO users ";
$query.= " (user_id, user_token)";
$query.= " VALUES (?, ?)";
$stmt = mysqli_prepare($dbLink, $query);
mysqli_stmt_bind_param($stmt, 'ss', $user_id, $token);
mysqli_stmt_execute($stmt);

在你的函数之外,这样你就不会在关闭它后使用该链接。

此外...如果您的函数递归...比方说 10 次,那么将该值插入 10 次是没有意义的,不是吗?

更简单的是,你只需添加一个else就可以将它插入到base case中:

function generateToken($user_id){
global $dbLink;

//generate Token with different numbers
$numbers = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$token = array();

while (count($token) < 6) {
$number = array_rand($numbers);
if (!in_array($number, $token)) {
array_push($token, $number);
}
}

$token = implode($token);

//check if token exists
$query = "SELECT * FROM users WHERE user_token = '{$token}' AND user_active = 1";
$result_set = mysqli_query($dbLink, $query);
if (mysqli_num_rows($result_set) != 0) {
generateToken($user_id);
}else{

//save token and user id in database

$query = "INSERT INTO users ";
$query.= " (user_id, user_token)";
$query.= " VALUES (?, ?)";
$stmt = mysqli_prepare($dbLink, $query);
mysqli_stmt_bind_param($stmt, 'ss', $user_id, $token);
mysqli_stmt_execute($stmt);
mysqli_close($dbLink);
}


return $token;

关于php - 递归函数和mysqli关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443235/

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