1

This is my code. can anyone try to figure out why is there an error.

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "fruitsandvegetables";
 $conn = new mysqli($servername, $username, $password, $dbname);
 if($conn->connect_error) {
die ("connection failed: " . $conn->connect_error);
 }
 $sql = "SELECT productID,productName,productPricePerKg,productStockKg,FROM      fruitsandvegetables";
 $result = $conn->query($sql);

 echo "<table style='width: 100%'>";
 echo "<tr>";
 echo"<th> productID</th>";
 echo "<th>productName</th>";
 echo "<th> productPricePerKg</th>";
 echo  "<th> productStockKg</th>";
 echo "</tr>";

 if ($result->num_rows > 0) {
     // output data of each row
while($row = $result->fetch_assoc()) {

    echo "<tr>";
    echo "<td> {$row["productID"]}</td>";

    echo"<td> {$row["productName"]}</td>";
    echo "<td>{$row["productPricePerKg"]}</td>";

    echo "<td>{$row["productStockKg"]}</td>";


    echo"</tr>";
     }

 } else {
     echo "0 results";
 }


 echo "</table>";
 $conn->close();

error on if($result->num_rows > 0){ trying to get property of non object

can anyone figure out why? thank you. the front end is php and the backend is sql

Nick
  • 138,499
  • 22
  • 57
  • 95
Mark
  • 11
  • 1
  • You have a `,` that shouldn't be there in `productStockKg,FROM` – Nick Nov 07 '18 at 07:57
  • 2
    The actual issue is that your query fails. That is why you do _not_ have a result object. However you do not realize that since you did not implement _any_ form or error detection or handling... – arkascha Nov 07 '18 at 07:58

1 Answers1

0

Hope I could help, try to include mysqli before the num_rows same with fetch_assoc to be sure.

Ex:

if ($result->mysqli_num_rows > 0) {
while($row = $result->mysqli_fetch_assoc()) {