0

I'm currently working on a PM system (private message system). However, I am getting a error.

The error is:

Notice: Trying to get property of non-object in C:\xampp\htdocs\brickmarket\messages.php on line 18

I have tested this without the WHERE to='" . $_SESSION["user_name"] . "'"; and that works fine and the error doesn't appear, so I am unsure what the issue is.

$_SESSION["user_name"] outputs the username of the currently logged in user.

My full code is:

<h1>My private messages</h1>
<?php
$sql = "SELECT * FROM pb WHERE to='" . $_SESSION["user_name"] . "'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    ?>
<table width="100%">
<tr>
    <td><strong>ID</strong></td><td><strong>From</strong></td><td><strong>To</strong></td><td><strong>Subject</strong></td><td><strong>Date</strong></td><td></td>
</tr>
    <?php
    while($row = $result->fetch_assoc()) {
    ?>
<tr>
    <td><?php echo $row["id"]; ?></td><td><?php echo $row["from"]; ?></td><td><?php echo $row["to"]; ?></td><td><?php echo $row["subject"]; ?></td><td><?php echo $row["date"]; ?></td><td><a href="">View</a></td>
</tr>
    <?php
    }
?>
</table>
<?php
} else {
    echo "You don't have any messages yet.";
}
?>

I hope someone can help me out, I'm fighting with my code for a few hours now.


PS: I know this code is vulnerable to SQL injection & will fix that later.

J. Doe
  • 31
  • 4
  • 1
    *"I have tested this without the WHERE to"* - That's because `to` is a MySQL reserved word and requires special treatment https://dev.mysql.com/doc/refman/5.5/en/keywords.html - http://dev.mysql.com/doc/en/identifier-qualifiers.html – Funk Forty Niner Apr 16 '16 at 13:00
  • @Fred-ii- Oh okay, learned something new today! – J. Doe Apr 16 '16 at 13:03
  • We've all been down that road and will still find new trails to adventure ourselves on ;-) *Cheers* – Funk Forty Niner Apr 16 '16 at 13:04

0 Answers0