问题描述
我想我在这里迷失了方向.
I think I'm losing my mind here.
这是代码. (这是我为了说明这一点而实际要做的工作的简化版本.)
This is the code. (It's a simplified version of what I am actually trying to do in order to demonstrate the point.)
$STH = $DBH->query("SELECT * FROM help");
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
echo $row['text'];
$help_text = $row['text'];
}
echo "->";
echo $help_text;
echo "<-";
使用句柄DBH到MySQL数据库的数据库连接很好(未列出).该查询工作正常.循环内$row['text']
的回声可以正常工作多次.但是,在->
和<-
之间的$help_text
回声不起作用,导致显示-><-
.我希望回显显示$row['text']
的最后一个实例.
The db connection to the MySQL db using the handle DBH is fine (not listed). The query works fine. The echo of $row['text']
within the loop works fine multiple times. However, the echo of $help_text
between ->
and <-
does nothing, resulting in -><-
being displayed. I would expect the echo to show the last instance of $row['text']
.
请问为什么这不起作用?!
Why is this not working, please?!
推荐答案
您需要在循环外声明它
$help_text = "";
while($row = $STH->fetch()) {
echo $row['text'];
$help_text .= $row['text'];
}
echo "->";
echo $help_text;
echo "<-";
这篇关于PHP-PDO提取循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!