How to return an array from ORM?

Hello All,

I am build a basic Database API (mainly MySQL) … I would like to do the following… (I want the idiomatic way)

while ($row = $sth->fetch (PDO::FETCH_OBJ))
     printf ("Name: %s, Category: %s\
", $row->name, $row->category);

Instead of the above I would want to create an associative array of arrrays that
would hold contain $row… I intend to return the associative array to my view.

Could someone help me with the best way to do this?


If you want to use the array notation ( ) instead of object ( -> ) then use FETCH_ASSOC or FETCH_BOTH in place of FETCH_OBJ.

The example given might be a bit wayward as it is using ->fetch() to get a single row, yet it is looping through the results as if you had used ->fetchAll().


My question is a bit unclear… What I really want is to fetch all once so that I can carry out looping inside my view which is Smarty.

Thanks for your support

Try this:

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

Now you can assign $rows to your Smarty template to loop through it there.