Click here to Skip to main content
15,920,617 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My input type in a form in html is datetime-local and keep in a table in mysql.
In my table in mysql, i created a column named deadline whose type is datetime.

I try to display a row which deadline is after now.
But, it shows the error "Trying to get property of non-object".
I don't understand why.

What I have tried:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "MyDB";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection


$sql = "SELECT Ass, Deadline, FROM Board WHERE Deadline >= NOW()";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
Assignment: ". $row["Ass"]. " - Deadline: ". $row["Deadline"]. "
";
}
} else {
echo "0 results";
}
$conn->close();
?>
Posted
Updated 30-May-18 4:49am
Comments
Richard MacCutchan 30-May-18 10:26am    
You need to check which line throws the error, and which variable is null.

1 solution

Always check for mysqli errors, especially during development because then you can get meaningful error messages:
PHP
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno) {
    printf("Connect failed: %s\n", $con->connect_error);
    exit();
}
if ($result = $conn->query($sql)) {
    // $result is an object and can be used to fetch row here
}
else {
    printf("Query failed: %s\n", $conn->error);
}

A possible error source is in your query string which has an unexpected comma between DeadLine and FROM. Such syntax errors result in a boolean FALSE be returned by the query() function instead of a mysqli_result object. And the boolean type is not an object type and has therefore no properties like num_rows.

See also PHP: mysqli::query - Manual[^]-
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900