SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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)

    PHP Code:
    while ($row $sth->fetch (PDO::FETCH_OBJ))
         
    printf ("Name: %s, Category: %s\n"$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?

    Regards,
    Emeka

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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().

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups,

    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

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Try this:

    PHP Code:
    $rows $sth->fetchAll(PDO::FETCH_ASSOC); 
    Now you can assign $rows to your Smarty template to loop through it there.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •