gpt4 book ai didi

PHP 从 mysql* 迁移到 mysqli

转载 作者:行者123 更新时间:2023-11-29 04:22:19 24 4
gpt4 key购买 nike

<分区>

我刚刚开始一个旧项目,我需要做的第一件事就是从 mysql_* 扩展迁移到 mysqli_* 扩展。我以前没怎么用过 PHP...大多数新代码都可以工作,但在下面的示例中我似乎把事情搞砸了...

旧函数:

function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

新(无效)功能:

function user_id_from_username($username) {
$username = sanitize($username);
$id = mysqli_query(connect(),"SELECT `user_id` FROM `users` WHERE `username` = '$username'");
return $id;
}

另一个旧的:

function login($username, $password) {
$user_id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) ==1) ? $user_id : FALSE;
}

还有新的:

function login($username, $password) {
$user_id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

$check = mysqli_query(connect(),"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'");
return $check == $user_id ? TRUE : FALSE;
}

我的清理功能:

function sanitize($data) {
return htmlentities(strip_tags(mysqli_real_escape_string(connect(), $data)));
}

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