Joshua Bakker
Joshua Bakker

Reputation: 2358

PDO::Query to array

I got a basic simple query to get data from a table. It doesn't need any parameters so I want to use a basic PDO::Query way.

I got this:

$items = $cmsDbh->query("SELECT * FROM `tbl1`");

Now this is my AJAX file, I want to return the data as a JSON object. I tried using:

echo json_encode($items);

But then I get a JSON object which contains the queryString (the SELECT * FROM tbl1).

I don't want to use PDO::Prepare, is there another way of doing it?

Upvotes: 0

Views: 152

Answers (2)

Monk Ren
Monk Ren

Reputation: 57

echo json_encode($items->fetchAll());

Upvotes: 1

Joshua Bakker
Joshua Bakker

Reputation: 2358

Was stupid. Appearantly PDO::Query returns a PDOStatement object, so you can just use fetchAll. Working code:

echo json_encode($items->fetchAll(PDO::FETCH_ASSOC))

Upvotes: 0

Related Questions