Arjan Oskam
Arjan Oskam

Reputation: 31

Can't Fetch All Rows

I want to get all room catagory's from the database! So that there are no doubles, as you can add multiple rooms with a specific catagory.

Here is my function:

public function loadRoomselect(){
    $sql = "SELECT DISTINCT Catagory FROM room";
    $sth = $this->pdo->prepare($sql);
    $sth->execute();
    $row = $sth->fetchObject();
    print_r($row); var_dump($row);
    echo("<label for='catagory'>Category:</label><select id='catagory' name='catagory'><select name='catagory' id='catagory'>");
    foreach($row as $catagory){
        var_dump($catagory);
        echo("<option name=" . $catagory . ">" . $catagory . "</option>");
    }
    echo("</select>");
}

For some reason it just gets the first catagory from the database! the Var_Dump gives:

object(stdClass)#4 (1) { ["Catagory"]=> string(6) "Single" 

What am I doing wrong?

Upvotes: 1

Views: 70

Answers (1)

andrew
andrew

Reputation: 9583

use:

$rows=  $sth->fetchAll(PDO::FETCH_OBJ);

instead of:

$row = $sth->fetchObject();

fetchObject only retrieves a single row

Edit

try this

foreach($rows as $category){
        var_dump($category);
        echo("<option name=\"{$category->Catagory}\">{$category->Catagory}</option>");
    }

Upvotes: 1

Related Questions