0

I want to display data gathered from a database in an html table using php and I'm having a problem by the name of:

Notice: Undefined index: filename in C:\Users\me\PhpstormProjects\untitled\page.php on line 39

I'm using PHP 7.4.26 + MySQL for database and I'm on Windows.

This is how the code looks:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

// Create database
$sql = "SELECT * FROM testdata";

$result = mysqli_query($conn, $sql);

$row = mysqli_fetch_assoc($result);

$conn->close();
?>

<html>
<style>
    table, th, td {
        border: 1px solid black;
    }
</style>
    <body>
        <table>
            <tr>
                <th>Filename</th>
                <th>Size</th>
            </tr>
            <tr>
                <td>
                    <?php echo $row["filename"] ?>
                </td>
                <td>
                    <?php echo $row["size"]; ?>
                </td>
            </tr>
        </table>
    </body>
</html>
LazyOne
  • 158,824
  • 45
  • 388
  • 391
Vonor22
  • 1
  • 1
  • 1
  • 1
    try `var_dump($row)` to see what's inside – Traian GEICU Feb 26 '22 at 17:12
  • 1
    Based on the error, your `$row` could be empty. Which would mean "no records in that table". Do the `var_dump($row)` thingy to see what is has -- 1) if it's an array and 2) if it has the `filename` field there. – LazyOne Feb 26 '22 at 17:31
  • I figured it out. You see, in the table the columns were written with capital letters while in my code I was referencing them with lowercase letters. I should've checked that. I'm sorry for wasting your time. This just shows that I need to be more careful in the future. Thank you for your help. I appreciate it. – Vonor22 Feb 26 '22 at 20:13

0 Answers0