Return a single value from a CI model method

I have a method that should return a single value. Since it ->result()returns an array, I use the following to return the single value that I would like to get:

return array_pop($this->db->query($SQL)->result())->event_name;

while it works fine, I'm wondering if there is something built into CI that maybe I skipped in the documentation. I also use this trick to return a single entry:

return array_pop($this->db->query($SQL)->result());

Is there a better way to do this?

+3
source share
2 answers

row_array()will return you the first string found as an array, and row()will return it as an object.

So instead array_popyou can use:

return $this->db->query($SQL)->row()->event_name;

OR

return $this->db->query($SQL)->row_array();

http://ellislab.com/codeigniter/user_guide/database/results.html

+11
source

, Rocket, , . , :

function getNumberOfArticles() {
    $result = $this->db->query("SELECT COUNT(id) AS returnVal FROM articles")->row_array();
    return $result['returnVal'];
}
+1

All Articles