Error: Trying to get property of non-object

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Error: Trying to get property of non-object

Postby remmargorp » 14. October 2015 14:15

Hi all,

The following code index.php is stored at C:\xampp\htdocs\xss\3\index.php.
When I try to run it, I'm getting this error. Please let me know how to fix this issue.

URL in my computer
Code: Select all
http://localhost/xss/3/


Error Message
Code: Select all
Notice: Trying to get property of non-object in C:\xampp\htdocs\xss\3\index.php on line 75


Line 75 of C:\xampp\htdocs\xss\3\index.php
Code: Select all
if ($result->num_rows > 0) {


Related code:
Code: Select all
<?php
$sql = "SELECT id, comment FROM comments";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td style='width:35%;padding:10px'>Comment #".$row["id"]."<br /><hr />".$row["comment"]."<br /></td></tr>";
    }
} else {
    echo "<tr><td style='width:35%'>No Comments!</td></tr>";
}
$conn->close();
?>


Please let me know if you need more info to resolve this issue.
remmargorp
 
Posts: 3
Joined: 14. October 2015 05:31
Operating System: Mac

Re: Error: Trying to get property of non-object

Postby Altrea » 14. October 2015 14:50

Where did you initialize $conn?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8299
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Error: Trying to get property of non-object

Postby Nobbie » 14. October 2015 15:04

You do not evaluate $result, it could be NULL.
Nobbie
 
Posts: 8780
Joined: 09. March 2008 13:04

Re: Error: Trying to get property of non-object

Postby Altrea » 14. October 2015 15:19

We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8299
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Error: Trying to get property of non-object

Postby remmargorp » 14. October 2015 17:54

Altrea wrote:Where did you initialize $conn?


Thanks Altrea for your feedback. I hope this will answer your question.
Code: Select all
[code]$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['clear']))
{
   $sql = "TRUNCATE TABLE comments";
   if ($conn->query($sql) === TRUE)
   {
      echo "Table Cleared";
   } else {
      echo "Error: Unable to Clear Table". $conn->error;
   }
}[/code]

<?php
if (isset($_POST['comment']))
{
   if ($conn->query($sql) === TRUE)
   {
      echo "New record created successfully";
   } else {
      echo "Error: Unable to add comment";
   }
}
?>
Last edited by remmargorp on 14. October 2015 17:57, edited 1 time in total.
remmargorp
 
Posts: 3
Joined: 14. October 2015 05:31
Operating System: Mac

Re: Error: Trying to get property of non-object

Postby remmargorp » 14. October 2015 17:57

Nobbie wrote:You do not evaluate $result, it could be NULL.


Thanks Nobbie for your input. I'm new to this. Please let me know how to evaluate it.
remmargorp
 
Posts: 3
Joined: 14. October 2015 05:31
Operating System: Mac

Re: Error: Trying to get property of non-object

Postby Nobbie » 14. October 2015 18:34

remmargorp wrote: Please let me know how to evaluate it.


With an if-clause and a comparison.

Am i right, that you have no idea about programming PHP? I cannot teach you PHP, you have to learn it on yourself.
Nobbie
 
Posts: 8780
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 47 guests