【问题标题】:PDO error Cannot use object of type stdClass as arrayPDO 错误不能使用 stdClass 类型的对象作为数组
【发布时间】:2016-05-07 19:54:36
【问题描述】:

我在使用 PHP 时一直在尝试学习 PDO 而不是 MySQL,我有一些代码我看不到错误在哪里:

<?php

include('includes/db_connection.php');
include('includes/sessions.php');
include('includes/functions.php');
include('includes/header.php');
include('includes/loginnav.php');

$row = DB::getInstance()->selectOneByField('membership', 'username', $member);

$logged_in_users_id = $row['id'];

$result3 = DB::getInstance()->select('
    SELECT  *
    FROM    `pms`
    WHERE   `sender_id` = :logged_in_users_id
    ORDER   BY `date_added` DESC',
[
    'logged_in_users_id' => $logged_in_users_id
]);

if (count($result3) == 0) {
    stderr('No messages have been sent. (<a href="inbox.php">Inbox</a>)');
}

?>

<div class="panel panel-primary">
    <div class="panel-heading">Sentbox</div>
    <div class="panel-body">
        <table class="table table-striped table-condensed table-responsive table-hover">
            <thead>
                <tr>
                    <th>Subject</td>
                    <th>Date Sent</td>
                    <th>To User</td>
                    <th>Read (<font color="green">Y</font>&nbsp;/&nbsp;<font color="red">N</font>)</td>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($result3 as $row) { ?>
                    <?php
                        $id = $row['id'];
                        $sendee_id = $row['reciever_id'];
                        $read = $row['read_flag'];
                        $row = DB::getInstance()->selectOneByField('membership', 'id', $sendee_id);
                        $sendee = $row['username'];
                        $sendee_id = $row['id'];
                        $row_2 = DB::getInstance()->selectOneByField('pms', 'reciever_id', $logged_in_users_id, PDO::FETCH_OBJ);
                    ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($row_2['subject']) ?></strong></td>
                        <td><?php echo htmlspecialchars($row_2['date_added']) ?></td>
                        <td><a href="user-details.php?id=<?php echo $sendee_id ?>"><?php echo htmlspecialchars($sendee) ?></td>
                        <td style="color: <?php echo ($read == 'Y')? 'green': 'red' ?>"><strong><?php echo htmlspecialchars($read) ?></strong></td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>

第 52 行的错误:“PHP 致命错误:无法使用 stdClass 类型的对象作为数组中的对象”,该行位于此行:

<td><strong><?php echo htmlspecialchars($row_2['subject']) ?></strong></td>

我试图调试错误是什么,但我卡在了这一刻,任何帮助将不胜感激。

【问题讨论】:

  • 错误告诉你问题出在哪里。 $row_2 是一个对象,但您正试图以数组的形式访问它。
  • ..换句话说,试试$row_2-&gt;subject
  • 谢谢米奇!效果很好:)

标签: php


【解决方案1】:

尝试$row_2-&gt;subject 而不是$row_2['subject']

访问 StdClass 对象时使用 ->.

【讨论】:

  • 感谢 Prax 一个菜鸟的错误!
猜你喜欢
  • 2019-12-11
  • 1970-01-01
  • 2021-12-17
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多