I'm making a simple private messaging system. I try to make all conversations appear in the user's inbox, I use a foreach loop for this. However, the loop doesn't seem to print out anything. It only gives the error: Warning: illegal string offset.
Below is an example of the code (in my original code I get these values from my database with a query)
$result = array();
$result['conversation_id'] = '3';
$result['conversation_subject'] = 'test112';
$result['conversation_last_reply'] = '1485701808';
print_r($result);
foreach($result as $conversation){
echo $conversation['conversation_subject'];
}
When I use print_r the correct results print (3, test112 and 1485701808) but in the foreach loop I keep getting the warning. Can anyone help me with this problem? Thanks!!
EDIT: relevant PHP code
$sql = $db->prepare("SELECT
conversations.conversation_id,
conversations.conversation_subject,
MAX(conversations_messages.message_date) AS conversation_last_reply
FROM conversations
LEFT JOIN conversations_messages ON conversations.conversation_id = conversations_messages.conversation_id
INNER JOIN conversations_members ON conversations.conversation_id = conversations_members.conversation_id
WHERE conversations_members.user_id = {$_SESSION['user_id']}
AND conversations_members.conversation_deleted = 0
GROUP BY conversations.conversation_id
ORDER BY conversation_last_reply DESC");
$sql->execute();
$result = $sql->fetch(PDO::FETCH_ASSOC);
foreach($result as $conversation){
?>
<div class="conversations">
<h4><a href=""><?php echo $conversation['conversation_subject']; ?> </a></h4>
<p>Last reply: </p>
</div>
<?php
}
EDIT: I also get 2 warnings when I try to echo the 'conversation_last_reply: date() expects parameter 2 to be integer, string given in AND Warning: illegal string offset.
This is what I tried to echo the date:
echo date('d/m/Y H:i:s', $conversation['conversation_last_reply']);