Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

}

share|improve this question

4 Answers 4

up vote 0 down vote accepted

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();
    }
}
share|improve this answer
    
this return the error - mysqli::query() [mysqli.query]: Empty query, can't explain why though –  Stender Apr 15 '14 at 13:31
    
still returns an error - mysqli_result::fetch_object() expects parameter 1 to be string –  Stender Apr 17 '14 at 15:28
    
Should be fixed. Sorry. Reference: us3.php.net/manual/en/mysqli-result.fetch-object.php –  Amadu Bah Apr 17 '14 at 15:57
    
this did it! thanks –  Stender Apr 19 '14 at 18:08

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

}

share|improve this answer
    
hmm, then it only displays the last object in the table, so all the other hotels are not being echoed. –  Stender Apr 15 '14 at 13:24

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";
    }  
}
share|improve this answer
    
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. –  Stender Apr 15 '14 at 13:20
$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";
}  

}

share|improve this answer
    
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? –  Stender Apr 15 '14 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.