0

I have this PHP code, which fetches data from my SQL database called "comments". This code prints out every comment in the table:

<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);

if($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<strong>ID:</strong><br>  " . $row["id"] . "<br>";
        echo "<strong>Navn:</strong><br>  " . $row["name"] . "<br>";
        echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
        echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
        echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
    }
    echo '<div class = "white_line_comments"></div>';
} else {
    echo "0 results";
}

This has worked fine so far, everything prints as it's supposed to. Then I decided I wanted a way to give each individual comment some sort of identification to make them unique. I tried putting each single comment into its own div, using the SQLtable row id as id for the div.

However, when I try to access my webpage now, it tells me the website doesn't work (HTTP Error 500).

<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);

if($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br>  " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br>  " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
    echo '
<div class="white_line_comments"></div>';
} else {
    echo "0 results";
}

Any ideas on this? I guess I must've done something wrong when including the div, but I can't figure out what!

empiric
  • 7,825
  • 7
  • 37
  • 48
mRambech
  • 13
  • 1

3 Answers3

2

You have an error in this line after starting while loop:

echo "<div class ='$row['id'].'>";

It should be

echo "<div class ='". $row['id'] ."'>";

You should also configure your web server/hosting/localhost to throw a PHP error.

Read this if you are on localhost or your own server: How can I make PHP display the error instead of giving me 500 Internal Server Error

Read this if you are using shared hosting: How do I displaying details of a PHP internal server error?

Community
  • 1
  • 1
Afif Zafri
  • 640
  • 1
  • 5
  • 11
0
 echo "<div class ='$row['id'].'>";

First line in while loop should look like this

echo "<div class = '".$row['id']."'>";

or

echo "<div class = '$row['id']'>"
bloodscript
  • 137
  • 12
0

You have mixed different apostrophe, try this:

echo "<div class ='" . $row['id'] . "'>";
Vizz85
  • 195
  • 1
  • 13