I am new to programming. I wrote a code to fetch last added record but it throwing an error.
Notice: Trying to get property of non-object in C:\xampp\htdocs\fetch.php on line 17 0 results
fetch.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "d4rky";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT TOP 1 * FROM data ORDER by id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["dataset"]. " " . $row["datamet"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Table name : data
Thanks in advance .