在使用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> / <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>
错误:“PHP致命错误:无法在第52行上使用stdClass类型的对象作为数组:
<td><strong><?php echo htmlspecialchars($row_2['subject']) ?></strong></td>
我试图调试错误是什么但是我被困在那一刻,任何帮助都将不胜感激。
答案 0 :(得分:2)
尝试使用$row_2->subject
代替$row_2['subject']
访问StdClass对象时,请使用 - &gt;。