如何正确使用MySQLI查询和获取?

时间:2017-02-12 06:01:42

标签: php mysql mysqli

所以我刚刚开始学习PHP并且正在按照一个旧的指南来制作我感兴趣的东西。整个指南都使用了旧的弃用的mysql函数,所以我决定冒险尝试PHP的小经验并尝试修复我自己。我决定迁移到mysqli,因为它似乎比PDO更容易替代。一切似乎都运行良好,直到我意识到我的用户登录不太正常。以下是我设置的方法:

$password = md5($_POST['user_password']);
$sql = "SELECT * FROM users
        WHERE user_email = '$user_email' 
        AND user_password = '$password'";
$result = mysqli_fetch_assoc($link->query($sql));

$_SESSION['user_id'] = $result['user_id'];
$_SESSION['user_acl_id'] = $result['user_acl_control_id'];
$_SESSION['user_first_name'] = $result['user_first_name'];
$_SESSION['user_last_name'] = $result['user_last_name'];`

每当我尝试通过登录输入密码时,即使它是正确的,它也会告诉我我错了。我怀疑我的问题在这一行内:

$result = mysqli_fetch_assoc($link->query($sql));

指南原来是这样的:

$password = md5($_POST['user_password']);

$sql = "SELECT * FROM users WHERE user_email = '$user_email' AND user_password = '$password'";
$result = mysql_fetch_assoc(mysql_query($sql));

$_SESSION['user_id'] = $result['user_id'];
$_SESSION['user_acl_id'] = $result['user_acl_control_id'];
$_SESSION['user_first_name'] = $result['user_first_name'];
$_SESSION['user_last_name'] = $result['user_last_name'];

如果有人能帮助我,我将不胜感激!提前谢谢!

1 个答案:

答案 0 :(得分:1)

这样说吧

$result = mysqli_fetch_assoc(mysqli_query($con,$sql));

$con替换为您的连接名称