PDO For Each 循环中的非法字符串偏移 [英] Illegal String Offset within PDO For Each loop

查看:30
本文介绍了PDO For Each 循环中的非法字符串偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 查询:

<?php

    $query = $db->prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5');
    $query->bindParam(":id",$id);
    $result = $query->execute();

    $rows = $query->fetch();

    foreach ($rows as $row) {
        echo $row["timestamp"]; "<br />";
    }

?>

应该打印的两行(时间戳):

The two rows that should be printed (timestamp):

实际打印的内容:1188((22

What actually prints: 1188((22

控制台中的错误:PHP 警告:第 73 行/Sites/pages/user_account.php 中的非法字符串偏移 'timestamp' - 第 73 行是 forloop 内的 echo $row....

The error within the console: PHP Warning: Illegal string offset 'timestamp' in /Sites/pages/user_account.php on line 73 - Line 73 being the echo $row... inside the forloop.

任何帮助将不胜感激.

推荐答案

您正在使用 fetch,而不是 fetchAll:

You are using fetch, which retrieves a single row, instead of fetchAll:

$rows = $query->fetchAll();

这篇关于PDO For Each 循环中的非法字符串偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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