PHP PDO获取行和打印值 [英] PHP PDO Fetch row and print values

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

问题描述

我无法理解PDO中fetch/fetchall的确切工作方式.我能否为PDO fetch提取一个简单的示例以获取每一行并迭代显示其值?

I couldn't understand the exact working of the fetch/fetchall in PDO.can i get a simple example for PDO fetch for fetching each row and displays its values iteratively?

推荐答案

周围有很多示例.您也可以使用while循环进行简单的提取

There are a lot of examples around.. You can do a simple fetch using a while loop too

<?php
//your PDO connection goes here.....
$stmt = $db->query('SELECT username FROM yourtable');

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['username'];
}

PDOStatement::fetch

通过使用fetchAll,您可以将所有结果存储在一个数组中.

By using a fetchAll , you can grab all the results in a single array..

$stmt = $db->query("SELECT username FROM yourtable");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);

PDOStatement::fetchAll

这篇关于PHP PDO获取行和打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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