簡體   English   中英

PDO錯誤無法將stdClass類型的對象用作數組

[英]PDO error Cannot use object of type stdClass as array

我一直在嘗試使用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->subject代替$row_2['subject']

訪問StdClass對象時,請使用->。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM