PDO提取/提取全部 [英] PDO fetch / fetchAll

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

问题描述

对于PHP来说不是新手,但是对PDO来说只有一天的时间.当然,我在这里做错了.

Not new to PHP, but only a day old to PDO. Surely I am doing something wrong here.

    $query = $conn->prepare("SELECT * FROM admins WHERE username = :username AND password = :password");
    $query->execute(array('username' => $username,'password' => sha1($password)));
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    if (count($result) > 0)
    {
        $_SESSION['user']['who']        = $result[0]['who'];
        $_SESSION['user']['email']  = $result[0]['email'];
        $_SESSION['user']['username']   = $result[0]['username'];
        echo REFRESH;
    }
    else
    {
        $message = "Invalid username / password.";  
    }

我可以正确声明那些$_SESSIONS的唯一方法是使用$result[0]['fieldName'];,如何通过$result['fieldName'];进行访问?

the only way I can declare those $_SESSIONS properly is if I use $result[0]['fieldName']; How can I just access it via $result['fieldName']; ?

推荐答案

fetchAll <如文档所述,/a>同时检索所有获取数据的数组.由于使用的是FETCH_ASSOC,因此将提取关联数组的数组.如果您确定只返回一个列或仅对返回的第一列感兴趣,请使用fetch.否则,您必须遍历fetchAll结果或执行已完成的操作.

fetchAll, as the documentation says, retrieves an array of all fetch data simultaneously. Since you are using FETCH_ASSOC, an array of associative arrays is fetched. If you know for certain that only one column is being returned or are only interested in the first column returned, just use fetch. Otherwise you have to iterate over the fetchAll results or do what you've done.

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

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