PHP, MySQL: can't explain this undefined index

2020-05-08 08:21发布

问题:

I'm getting an undefined index error with this code:

// Select all bookings
$sql = "SELECT * FROM booking";

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

echo "<p>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // echo $row['custName'] . " - " . $row['roomNb'] . " - " . $row['date'];
    echo $row['custName'];
}

Error:

Notice: Undefined index: custName in C:\xampp\htdocs\alxbook\index.php on line 40

I'm selecting all columns from my table. I have no problem with $row['roomNb'] and $row['date'] but for some reason $row['custName'] is giving me problems. The spelling of custName is correct.

What could be causing this?

回答1:

Put a piece of debug code into your script to proove that the name is correct:-

// Select all bookings
$sql = "SELECT * FROM booking";

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

echo "<p>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    print_r( $row );  // debug code

    // echo $row['custName'] . " - " . $row['roomNb'] . " - " . $row['date'];
    echo $row['custName'];
}


标签: php mysql mysqli