#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171

    Return query results and num rows


    Hi;

    What is the best way to return query results and number of rows? I do this but I dont really like it.
    PHP Code:
    public function list_messages($id)
        {
            
    $this->db->select('message.content,
            message.clicked,
            message.title,
            message.date_added,
            message.time_added '
    );
            
    $this->db->from('message');
            
    $this->db->where('receiver_ID'$id);
            
    $query $this->db->get();
            
    $results[] = $query->result();
            
    $results['rows'] = $query->num_rows();
            return 
    $results;
        } 
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Why don't you like that? That's how I'd do it.

    Unless you need to paginate this data, then it's wasteful.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    Originally Posted by teammlopez
    mysql_fetch_array() and mysql_num_row() two function to fetch query and number of rows.
    neither of which work if you're not using the mysql_* functions (which you shouldn't be anymore)

    mysqli and PDO both have a method for getting the row count and CodeIgnitor (the framework used by zxcvbnm) has built an interface to whichever one CI uses.

    Back to the code, see this part:

    PHP Code:
    //
    $results[] = $query->result(); 
    $results['rows'] = $query->num_rows(); 
    I do something similar, but i whould have

    PHP Code:
    //
    $results['data'] = $query->result(); 
    $results['meta']['rows'] = $query->num_rows(); 
    this way I have an associative array at the top level with the result data in one element (which would ne a numeric zero indexed array) and meta data about the query in another (which would be associative again)..I just prefer this approach to mixing array key types (you would have 0 and rows as keys)

    But you could put all this into an object using stdClass, which could* improve memory performance if passing the object around

    eg

    PHP Code:
    $results = new stdClass;

    $results->data $query->result();

    $results->meta = new stdClass;

    $results->meta->rows $query->num_rows(); 
    *as objects are assiged by reference when assigned to another variable whereas other types are cloned when assigned to another variable
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by ManiacDan
    Why don't you like that? That's how I'd do it.
    Don't like that it returns the elements are the different types.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo