0

I can't figure out why my code isn't working? When i run it the page is blank!

This is a Harry Potter Database. I am trying to put the data within my SQL Database in a HTML Table. Sounds easy but I can't figure out why my code is not working!

<!DOCTYPE html>
<html>
<head>
    <title> Harry Potter Database </table>
</head>
<body>
<table>
    <tr>
        <th> ID </th>
        <th> Name </th>
        <th> Age </th>
        <th> Website </th>
    </tr>
<?php
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); error_reporting(E_ALL); 
$conn = mysqli_connect("localhost", "root", "", "survey");
$sql = "SELECT * FROM result";
$res = $conn->query($sql);

if($res->num_rows > 0)
{
    while($row = $res->fetch_assoc())
    {
        echo "<tr>
        <td>". $row["ID"] . "</td>
        <td>". $row["Name"] . "</td>
        <td>". $row["Age"] . "</td>
        <td>". $row["Website"] . "</td>
        </tr>";
    }
}
else{
    echo "No RESULTS";
}
$conn->close();

?>
</table>
</body>
</html>

result is the name data within my database called survey

Undefined array key "ID" in C:\xampp\htdocs\ws5\sqlquery.php on line 26

This is the error in the source

Structure of Table

ID WEBSITE AGE WEBSITE
4
  • Please add the stucture of your table to your question. Commented Mar 14, 2022 at 23:19
  • What database engine are you using (e.g. MySQL, PostgreSQL)? Commented Mar 14, 2022 at 23:24
  • I am using mySQL Commented Mar 14, 2022 at 23:24
  • Use dump_var($row); in order to debug the content of row. Commented Mar 14, 2022 at 23:25

1 Answer 1

1

On Windows (where it looks like you are), MySQL stores all table and column names in lowercase by default (see the docs for "Identifier Case Sensitivity" and lower_case_table_names).

That means that instead of "ID", you should be using "id", instead of "Name" it should be "name", etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I now have no errors in the source code. But when I run the host page I have no table whatsover?
I also made a mistake with the heading! Such a noob mistake. I put table instead of title :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.