• PHP Help
  • Notice: Undefined index on XAMPP. Please advise

I feel like I'm missing something here.

Notice: Undefined index: Products/Services in C:\xampp\htdocs\db\npcphptext.php on line 22

I cannot for the life of me work out what is wrong with line 22, and I have googled around and asked friends to no avail, as well as looking on here.

Please somebody enlighten me. I posted this on stackoverflow and it was taken straight down for being a duplicate...

<?php
$con=mysqli_connect("localhost","root","13151083","NPC");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM NPC");

echo "<table border='1'>
<tr>
<th>id</th>
<th>Products or Services</th>
<th>Price</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Products or Services'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>
I have been advised: 'var_dump($row); at the top of your while loop to find out what indexes it contains' and: 'The key Products/Services (or Products or Services, your code is different than your error message) does not exist in $row. You need to look at the contents of $row to find out what keys do exist'

but I am a noob and only understand plain English, essentially.

    In this case, the indexes for the $row array should exactly match the relevant column names in the database table being queried, and they will be case-sensitive. Since we do not know what the exact definition of that table is in your database, we can only tell you to look at it, and make sure you are using the right spelling (including underscores instead of spaces, if that is the case).

    You can stick in this bit of debug code to see exactly what is being retrieved (and then remove it when you are done debugging 😉 ).

    while($row = mysqli_fetch_array($result))
    {
      // add this just for debugging now:
      die("<pre>DEBUG:\n".var_export($row, 1).'</pre>');

      PS: I generally prefer within the SQL to write out the list of columns being retrieved from the DB, even if you really do want all of them (i.e. using "*"), just to make it clearer in the code what the relevant column names will be. (And especially do this if you do not need all of the columns retrieved, in order to be more efficient.) For example in this case:

      "SELECT id, Products_or_Services, Price FROM NPC"

      (using whatever the actual column names are where I just guessed what they are)

        NogDog has offered a lot of useful detail, I just want to add that "Undefined index" errors occur any time you have an array and attempt to refer to some array element that has not been defined. The function mysql_fetch_array returns array objects and these arrays will depend on the query you ran. I'm guessing your database doesn't have a column named Products or Services.

        This script will also cause an 'Undefined Index' error:

        <?php
        
        $my_array = array(
                "col1" => "this is column one",
                "col2" => "this is column two"
        );
        
        echo $my_array["this_key_has_not_been_defined"];

        The error is: PHP Notice: Undefined index: this_key_has_not_been_defined in /tmp/foo/foo.php on line 8

          So, the lesson for today: names have meanings, and they're unique.

          dalecosp
          Part of one of the Two Big Problems in programming: naming things, cache invalidation, and off-by-one errors.

          Write a Reply...