Mysqli到PDO的转换 [英] Mysqli to PDO conversion

查看:27
本文介绍了Mysqli到PDO的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我最近开始使用PDO,并且正在更改我的所有mysqli代码,但我似乎遇到了麻烦.现在,当我使用mysqli时,这段代码可以很好地工作,但是现在看来似乎无法打印出结果了.此代码基本上采用password的输入值,并与数据库中的哈希密码的输入值匹配,如果两者相等,则用户将登录.我的问题是我似乎无法找到一种获取方法我数据库中的密码.任何帮助将非常感激.谢谢.

I recently started using PDO a few days ago and am changing all my mysqli code but I seem to have hit a brick wall. Now this code worked beautifully when I was using mysqli, but now I can't seem to print out the result. This code basically takes the input value of password, and that of the hashed password in the database, matches them and if both are equal then the user will be logged in. My problem is that I can't seem to find a way to get the password from my database. Any help would be much appreciated. Thank you.

<?php 
 session_start();

 $user = "root";
 $pass = "";

$mcon = new PDO('mysql:host=localhost;dbname=rabbit_users', $user, $pass);

 try {
 $mcon = new PDO('mysql:host=localhost;dbname=rabbit_users', $user, $pass);
$mcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();



//prepare statement
$password = $_POST['password']; 

$stmt = $mcon->prepare("SELECT `password` FROM members WHERE password=:password");
$stmt->bindParam(":password", $_POST['password']);
$stmt->execute();

//get_result
$data_array = $stmt->fetchAll();
$stmt->fetch(PDO::FETCH_ASSOC);

//echo passwords
print 'Password from form: ' . $password . '<br />';
print 'Password from DB: ' . $data_array['password'] . '<br />';

//verify password
if (password_verify($password, $data_array['password'])) {
    print 'success';
    exit();
}else{
    print 'Try again m9';
    exit();
}

推荐答案

fetchAll 返回包含所有结果集行的数组.因此,如果使用$data_array[0]['password'],则可以使用$data_array[0]['password']对其进行访问. 您可能需要改用获取.

fetchAll returns an array containing all of the result set rows. So you can access to password with $data_array[0]['password'] if you used it. You may want use fetch instead.

$data_array = $stmt->fetch(PDO::FETCH_ASSOC);

这篇关于Mysqli到PDO的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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