PHP, MySQL: can't explain this undefined index error

冷暖自知 提交于 2019-12-31 06:37:27

问题


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'];
}


来源:https://stackoverflow.com/questions/18488233/php-mysql-cant-explain-this-undefined-index-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!