Php 如何读取fetch(PDO::fetch_ASSOC);

Php 如何读取fetch(PDO::fetch_ASSOC);,php,pdo,Php,Pdo,我正在尝试使用PHP构建一个web应用程序,并使用memcached存储数据库中的用户数据 例如,假设我有以下代码: $sql = "SELECT * FROM users WHERE user_id = :user_id"; $stmt = $this->_db->prepare($sql); $result = $stmt->execute(array(":user_id" => $user_id)); $user = $stmt->fetch(

我正在尝试使用PHP构建一个web应用程序,并使用memcached存储数据库中的用户数据

例如,假设我有以下代码:

 $sql    = "SELECT * FROM users WHERE user_id = :user_id";
$stmt   = $this->_db->prepare($sql);
$result = $stmt->execute(array(":user_id" => $user_id));
$user   = $stmt->fetch(PDO::FETCH_ASSOC);
我不确定如何读取
$user
变量并从中获取数据。我需要能够阅读电子邮件和密码栏

我希望有人能向我解释这是怎么回事


感谢阅读结果,您可以像阅读简单的php数组一样阅读它

例如,可以像
$user['name']
这样获取
名称。方法
fetch(PDO::fetch\u ASSOC)
将只返回1个元组tho。如果要获取所有元组,可以使用
fetchall(PDO::FETCH\u ASSOC)
。您可以遍历多维数组,得到相同的值。

从结果集中返回一行。该参数告诉PDO以关联数组的形式返回结果

数组键将与列名匹配。如果表包含“email”和“password”列,则数组的结构如下:

Array
(
    [email] => 'youremail@yourhost.com'
    [password] => 'yourpassword'
)
要从“电子邮件”列读取数据,请执行以下操作:

$user['email'];
至于‘密码’:

$user['password'];

PDO:FETCH_ASSOC
将结果放入一个数组中,其中值映射到其字段名

您可以像这样访问
name
字段:
$user['name']


我建议使用
PDO::FETCH_OBJ
。它获取对象中的字段,您可以如下方式访问:
$user->name

像任何其他关联数组一样在数组中循环:

while($data = $datas->fetch( PDO::FETCH_ASSOC )){ 
     print $data['title'].'<br>'; 
}
while($data=$datas->fetch(PDO::fetch_ASSOC)){
打印$data['title']。
; }

$resultset=$datas->fetchALL(PDO::FETCH_ASSOC);
回显“.$resultset.”;
方法

$email = $user['email'];
$password = $user['password'];
返回一个字典。您只需获取电子邮件和密码:

$users = $stmt->fetchall(PDO::FETCH_ASSOC);
其他方法

class Gateway
{
    protected $connection = null;

    public function __construct()
    {
        $this->connection = new PDO("mysql:host=localhost; dbname=db_users", 'root', '');
    }

    public function loadAll()
    {
        $sql = 'SELECT * FROM users';
        $rows = $this->connection->query($sql);

        return $rows;
    }

    public function loadById($id)
    {
        $sql = 'SELECT * FROM users WHERE user_id = ' . (int) $id;
        $result = $this->connection->query($sql);

        return $result->fetch(PDO::FETCH_ASSOC);
        // http://php.net/manual/en/pdostatement.fetch.php //                   
    }
}
返回设计模式“表数据网关”的字典的列表*/

/*仅打印列为“user\u id”的所有行*/

$user = $gateway->loadById(1);

$no = 1;
foreach ($user as $key => $value) {
    echo $no . '. ' . $key . ' => ' . $value . '<br />';
    $no++;
}

使用
print\r($user)
查看内容returnedIt返回一个数组,那么这是否意味着我只是读取了$user['password']?@PHPDEV:yes。正是:)谢谢大家。我对PDO的了解不是很好,现在我完全理解了如何阅读这些东西。它不是“像一个数组”,而是一个数组。另外,tuple
不是PHP术语:)如何从返回的数据中获取行数?@PHPDEV实际上不需要行count@George康明斯:很抱歉在几年后看到并反驳了您的评论:rowcount()仅用于计算受影响的行(插入、更新、删除)。要计算结果集,它可能与某些数据库(如mysql)一起工作,但根本不能保证,请参阅。使用
fetchAll()
然后使用
count()
对数组条目进行计数,或者如果计数是唯一需要的值,则查询
select count(*)
。这比4年前接受的这个问题的答案好多少?最好使用“列表”和“字典”来代替“数组”。“关联数组”是“dictionary”的合适词:)鉴于我们专门讨论PHP,术语
list
dictionary
完全不正确。PHP既没有列表也没有字典。它有数组和关联数组。
class Gateway
{
    protected $connection = null;

    public function __construct()
    {
        $this->connection = new PDO("mysql:host=localhost; dbname=db_users", 'root', '');
    }

    public function loadAll()
    {
        $sql = 'SELECT * FROM users';
        $rows = $this->connection->query($sql);

        return $rows;
    }

    public function loadById($id)
    {
        $sql = 'SELECT * FROM users WHERE user_id = ' . (int) $id;
        $result = $this->connection->query($sql);

        return $result->fetch(PDO::FETCH_ASSOC);
        // http://php.net/manual/en/pdostatement.fetch.php //                   
    }
}
$gateway  = new Gateway();
$users    = $gateway->loadAll();

$no = 1;
foreach ($users as $key => $value) {
    echo $no . '. ' . $key . ' => ' . $value['user_id'] . '<br />';
    $no++;
}
$user = $gateway->loadById(1);

$no = 1;
foreach ($user as $key => $value) {
    echo $no . '. ' . $key . ' => ' . $value . '<br />';
    $no++;
}
$user = $gateway->loadById(1);

echo $user['email'];
echo $user['password'];