I'm trying to make a foreach loop, that runs through the Db table, and echoes it out in a function, but when i try to do this, it takes the rows i have in the object, and echoes the object out as many times as i have rows, and doesn't proceed to the next object in the table. help is appreciated. Here is my code:
function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";
$result = $mysqli->query($sql);
$row = $result->fetch_object();
foreach ($row as $value) {
$foundhotel = "<h1>" . $row->hotel_name . "</h1></br>";
$foundhotel.= $row->hotel_adress . "</br>";
$foundhotel.= $row->hotel_postal_code . "</br>";
$foundhotel.= $row->description;
echo "$foundhotel";
}
}
tried doing this aswell, but this only displays the Last hotel in the table.
function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";
$result = $mysqli->query($sql);
$row = $result->fetch_object();
while($row = $result->fetch_object()){
$foundhotel = "<h1>" . $row->hotel_name . "</h1></br>";
$foundhotel.= $row->hotel_adress . "</br>";
$foundhotel.= $row->hotel_postal_code . "</br>";
$foundhotel.= $row->description;
echo "$foundhotel";
}
}