0

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";
    }  

}

4 Answers 4

2

->fetch_object retrieves only one row

Try this:

function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";
$result = $mysqli->query($sql);

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";
}  

}

Sign up to request clarification or add additional context in comments.

1 Comment

hmm, then it only displays the last object in the table, so all the other hotels are not being echoed.
0

fetch_object needs to be called for each row fetch. Try the following:

function hotels_from_db() {
    include 'DbConnection.php';
    $sql = "select * from hotels";

    if ($result = $mysqli->query($sql)) {

        /* fetch object array */
        while ($obj = $result->fetch_object()) {
            $foundhotel = "<h1>" . $obj->hotel_name . "</h1></br>";
            $foundhotel.= $obj->hotel_adress . "</br>";
            $foundhotel.= $obj->hotel_postal_code . "</br>";
            $foundhotel.= $obj->description;
            echo "$foundhotel";        
        }

        /* free result set */
        $result->close();
    }
}

3 Comments

this return the error - mysqli::query() [mysqli.query]: Empty query, can't explain why though
still returns an error - mysqli_result::fetch_object() expects parameter 1 to be string
Should be fixed. Sorry. Reference: us3.php.net/manual/en/mysqli-result.fetch-object.php
0

It should be:

function hotels_from_db() {
    include 'DbConnection.php';
    $sql = "select * from hotels";

    $result = $mysqli->query($sql);

    $row = $result->fetch_all();

    foreach ($row as $value) {   
        $foundhotel = "<h1>" . $value->hotel_name . "</h1></br>";
        $foundhotel.= $value->hotel_adress . "</br>";
        $foundhotel.= $value->hotel_postal_code . "</br>";
        $foundhotel.= $value->description;
        echo "$foundhotel";
    }  
}

1 Comment

thanks for the quick comment, but i have tried this, and then it just makes a whole lof of white space, and nothing from the db comes on the screen.
0
$row = $result->fetch_all(MYSQLI_ASSOC); // MAKE ARRAY ASSOCIATIVE ARRAY

foreach ($row as $value) {   
    $foundhotel = "<h1>" . $value->hotel_name . "</h1></br>";
    $foundhotel.= $value->hotel_adress . "</br>";
    $foundhotel.= $value->hotel_postal_code . "</br>";
    $foundhotel.= $value->description;
    echo "$foundhotel";
}  

}

1 Comment

hmm, this returns a Fatal error: Call to a member function fetch_all() on a non-object, but i see your point, assoc should mean that is checks the next array aswell right?

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.