试图从mysqli转换为pdo

时间:2016-03-17 10:09:26

标签: php mysql mysqli pdo

swagger project create sample-api

尝试将我的代码从MySQLi转换为PDO并遇到问题..试图获取查询中的所有信息并验证用户密码然后回显其余信息,(对于一个虚幻的项目)尝试了几个解决方案php文档和stackoverflow,但它们通常只是用于向mysql服务器发送信息。

2 个答案:

答案 0 :(得分:2)

select s.*, ss.*
from student s
left join student_story ss on ss.studentid = s.userid
where ss.new_id = ( select ss.new_id from student s
    group by dateline
    order by dateline desc
    limit 1
)

我就是这样做的。

答案 1 :(得分:0)

仅为了完整性:您似乎已尝试通过$stmt->execute(array(...))绑定结果字段。这不是PDO的工作原理 如果你想这样做,你必须使用PDOStatement::bindColumn之类的例如。

$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
    PDO::ATTR_EMULATE_PREPARES=>false,
    PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo);

$username = $_GET["user"];
$password = $_GET["passwd"];

$query = sprintf(
    "SELECT username, `password`, loginreqkey, banned FROM users WHERE username='%s'",
    $pdo->quote($username)
);
$stmt = $pdo->query($query);
$stmt->bindColumn(1, $username);
$stmt->bindColumn(2, $password);
$stmt->bindColumn(3, $loginreqkey);
$stmt->bindColumn(4, $banned);

if ( !$stmt->fetch(PDO::FETCH_BOUND) ) {
    noSuchRecordHandler();
}
else if (!password_verify($password, $result['password'])) {
    wrongPasswordHandler();
}
else {
    // would be "nice" if there was something like header('Content-type: application/json'); prior to sending the json data
    echo json_encode([
        "state" => "success",
        "loginreqkey" => loginreqkey,
        "banstatus" => $banned,
    ]);
}

但我建议按照SGR's回答中的说明进行操作。