duancan8382 2012-01-05 08:10
浏览 116
已采纳

PHP - PDO获取循环

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 "<-";

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?!

  • 写回答

1条回答 默认 最新

  • dousou1967 2012-01-05 08:17
    关注

    You need to declare it outside the loop

    $help_text = "";
    while($row = $STH->fetch()) {
        echo $row['text'];
        $help_text .= $row['text'];
    }
    echo "->";
    echo $help_text;
    echo "<-";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信