0

I have been trying to echo out database data through a while loop now for awhile but it doesn't work, I can't find where the issue is. I have tried echoing out data manually and that works just fine.

<?php $results = mysqli_query($con,"SELECT * FROM guestbook ORDER BY id DESC"); ?>


<?php while ($row = mysqli_fetch_assoc($results)) : ?>
   <li>
       <?php echo $row['message']; ?>
   </li>
<?php endwhile ?>
Kryddan
  • 15
  • 4
  • any error message? – Badiparmagi Mar 02 '18 at 08:36
  • nope I get nothing – Kryddan Mar 02 '18 at 08:37
  • So if your provided code works, what is the issue? – TimBrownlaw Mar 02 '18 at 08:40
  • it doesn't work thats the thing, nothing happends, it is almost like $row doesn't exist – Kryddan Mar 02 '18 at 08:42
  • So what if you add in echo "hello
    "; inside the while loop... Does that appear? Put it after the echo $row. Put another echo something after it... Does that show? This is Just to test that the code is running...
    – TimBrownlaw Mar 02 '18 at 08:44
  • For good measure to see if you are getting any results quickly and to see what your query is returning - add in a var_dump($row) does that show anything? – TimBrownlaw Mar 02 '18 at 08:45
  • I tried adding a bunch of echos and only the ones outside of the while loop works, var_dump didn't return anything either – Kryddan Mar 02 '18 at 08:49
  • So your error is in the while loop and your query isn't working...Ok well dont stop there, what does a var_dump($result); just after you set it, show you? I'd be suspicious of $con, can you echo that and view it... – TimBrownlaw Mar 02 '18 at 08:55
  • the var_dump then returns object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(7) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } – Kryddan Mar 02 '18 at 08:57
  • Have you got error reporting enabled... and oldie but a goodie - https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – TimBrownlaw Mar 02 '18 at 08:58
  • Ok, if you have phpmyadmin or something you can run SQL statements against your database, what does SELECT * FROM guestbook ORDER BY id DESC give you? – TimBrownlaw Mar 02 '18 at 08:59
  • I might be stupid or something but what do you mean exactly? I just started with sql and php – Kryddan Mar 02 '18 at 09:02

1 Answers1

0

First of all make sure you are connected to the database. I don't know if you omitted that on purpose or not. Also, check if the connection is established.

<?php

//Insert your server info here
$servername = "servername";
$username = "root";
$password = "root";
$dbname = "test_database";

// Create and check your connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM guestbook ORDER BY id DESC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo '<li>' . row["message"] . '</li>';
    }
} else {
    // Your query produced 0 results
    echo "Empty result!";
}

// Remember to close the connection
mysqli_close($conn);
?>
Marco
  • 3
  • 1
  • 3