I am able to retrieve and display one row of data from a database however I would like to retrieve more than one. Code I have is messed up but I think I am going in the right area just need to make this work.
My Code:
$query = "SELECT * FROM hotel";
$results = $conn->query($query);
while($hotel = $results->fetch()) { echo "<br> id: ". $hotel['hotel_id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " .
$hotel['postcode']. " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . <img src='". $hotel['image']. "'>"";
}
$conn=NULL;
?>
I see that you're using PDO. You can simply iterate through the results like this.
while($results_disp = $results->fetch(PDO::FETCH_OBJ))
{
//access rows here in this manner
echo "Hotel Id :- " . $results_disp->id;
}
while($hotel = $results->fetch()) {
echo "<br> id: ". $hotel['id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " . $hotel['postcode'].
" - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . $hotel['image'];
}
Replace your while loop with this .
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']. "'>" ....
}