Oto Shavadze
Oto Shavadze

Reputation: 42763

Obtain array result type, from PDO's query method

When We have WHERE condition in SELECT query, we can use PDO's prepare statement:

    $sth = $db->prepare("SELECT name FROM mytable WHERE id > :id");
    $sth->execute( array(":id"=>2) );
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);

So we obtain variable $result which type is array.

But when we dont have WHERE condition, we dont need prepare statement right? we use only query

$result = $db->query("SELECT name FROM books");

but now, $result type is not array, but pdostatement.

What is best way, for obtain also array types (and not pdostatement) in situations like this?

Upvotes: 0

Views: 77

Answers (1)

Rob
Rob

Reputation: 12872

$result = $db->query("SELECT name FROM books")->fetchAll(PDO::FETCH_ASSOC);

In other words, $db->query() does both prepare() and execute()

Upvotes: 1

Related Questions