emeka_micro — 2011-01-12T06:23:17-05:00 — #1
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?
cups — 2011-01-12T07:23:20-05:00 — #2
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().
emeka_micro — 2011-01-12T09:01:11-05:00 — #3
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
immerse — 2011-01-12T10:36:31-05:00 — #4
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
Now you can assign $rows to your Smarty template to loop through it there.