There are a few things wrong with your code:
$query = "SELECT * FROM hotel";
$results = $conn->query($query);
$hotel = $results->fetch();
At this point, you've loaded the first record into $hotel
. You don't need to do this if you're wanting to iterate through the recordset.
$hotel[$id] = $hotel['hotel_id'];
$hotel[$name] = $hotel['name'];
$hotel[$address] = $hotel['address'];
$hotel[$postcode] = $hotel['postcode'];
$hotel[$town] = $hotel['town'];
$hotel[$description] = $hotel['description'];
$hotel[$rating] = $hotel['rating'];
$hotel[$image] = $hotel['image'];
These lines are somewhat redundant. You've already got the details of the row in $hotel
, and you're adding extra entries using array keys that have not been defined.
/*
$id = $hotel['hotel_id'];
$name = $hotel['name'];
$address = $hotel['address'];
$postcode = $hotel['postcode'];
$town = $hotel['town'];
$description = $hotel['description'];
$rating = $hotel['rating'];
$image = $hotel['image'];
*/
while($hotel = $results->fetch()) {
echo "<br> id: ". $hotel[$id] = $hotel[id]. " - Name: ". $hotel[$name]. " - Address: " . $hotel[$address] . " - Postcode: " . $hotel[$postcode].
" - Town: " . $hotel[$town]. " - Description: ". $hotel[$description]. " - rating: ". $hotel[$rating]. " - image: " . $hotel[$image];
}
Inside your loop, you're trying to access array keys incorrectly. $hotel[$id]
is using $id
as the key, and that variable doesn't exist. $hotel[id]
is using an unquoted key, which PHP will guess is $hotel['id']
, but will complain about. So you can avoid the warning by quoting it directly.
So you can simplify your code down to:
$query = "SELECT * FROM hotel";
$results = $conn->query($query);
while($hotel = $results->fetch()) {
echo "<br> id: ". $hotel['id'] . " - Image: <img src='". $hotel['image']. "'>" ....
}