Returning Single Value from a Query |
[eluser]Unknown[/eluser]
I want to return just a single value from a COUNT query. Is the below code the shortest way to do that? Seems like there should be more of a shortcut. Code: $query = $this->db->query('SELECT COUNT(*) FROM users'); If there is no shortcut to the above, how can I extend the Database class to create a new method to accomplish this?
[eluser]stuffradio[/eluser]
Why are you doing it that way? Code: $query = $this->db->get('users');
[eluser]Hannes Nevalainen[/eluser]
Code: //Return the number of rows in $table_name. Details kan be found here-> Active Record - User Guide. Hope this helps //Hannes
[eluser]stuffradio[/eluser]
He asked for a simple, short way of doing it. Not a long way of doing it ![]()
[eluser]Unknown[/eluser]
Thanks for both examples. Actually, I wasn't specifically looking at the COUNT function necessarily, but just the quickest way to return a single value from a query. Here is another example query. Code: $query = $this->db->query('SELECT SUM(points) FROM users'); It would be great to just have a method like this: Code: $sum = $this->db->query_value('SELECT SUM(points) FROM users'); Anyway, after looking around, it seems that the DB class cannot be extended, but I'll have to create a model for this kind of thing. |
Welcome Guest, Not a member yet? Register Sign In |