【发布时间】:2015-06-16 21:53:10
【问题描述】:
Mytable:
+-----+-----------+
| id | word |
|-----|-----------|
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
+-----+-----------+
Mysql:
while($end = mysql_fetch_assoc($result)){
echo $end["word"].' - ';
}
输出:
test1 - test2 - test3
PDO:
$result = $sth->fetchAll();
print_r($result);
输出:它是两个嵌套数组,如果我想选择一条记录,我应该这样做:
echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
echo $result[2][word]; // output=test3
现在我想知道,有什么技巧可以让我回显 PDO 中列的所有记录吗?
编辑:
如何以非手动方式回显我的数据。有手动的:
echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
echo $result[2][word]; // output=test3
那么我应该为 1000 行做什么?我当然不应该这样做:
echo $result[0][word]; // output=test1
echo $result[1][word]; // output=test2
.
.
.
echo $result[999][word]; // output=test1000
有什么优惠吗?
【问题讨论】:
-
我看到
mysql_fetch_assoc()然后我看到fetchAll()。mysql_和 PDO 不能同时使用。您必须从连接到查询保持相同的 MySQL。或者,这是两者之间的原因/比较问题差异吗? -
@Fred-ii- 我在两个不同的示例中使用了
mysql_fetch_assoc()和fetchAll()。 -
你的问题还不清楚。如果您可以为未来的读者编辑您的问题,那将是最好的,以提及“使用两种不同的 API 有什么区别”类型的事情 ;-) 我认为你正在混合 MySQL API。有些人可能也不明白这个问题。
-
请阅读手册php.net/manual/en/pdostatement.fetch.php 里面都有。
-
@Fred-ii- 我已经读过了,我不想要数组中的结果。如果 pdo,我如何访问记录数据?事实上,在 POD 中什么是等价的:while($end = mysql_fetch_assoc($result)){echo $end["word"];} ?