0

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
  • Column does not exists in table, it seems. Show your table's structure. Commented Aug 28, 2013 at 12:43

1 Answer 1

2

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'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this. It appears the column name contained a space at the beginning. Very confusing as the space char does not appear in phpmyadmin! (that is probably a bug no?) So it was a typo from when I created the tables. But by using your debug code the space char does appear. Thank you
@user2018084 I would remove the space from the column name rather than put something into php to remove it.

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.