how to access the count(*) property? |
[eluser]seanloving[/eluser]
One of my database queries starts like this: Code: $row = $this->db->select('count(*), name, address, telephone') Code: $name = $row->name; What is the proper syntax to access the count(*) property? Thanks Sean Loving
[eluser]Unknown[/eluser]
Instead of: Code: $quantity = $row->count(*); Try to use this form: Code: $quantity = $this->db->count_all_results();
[eluser]sandeep nami[/eluser]
You can use these functions and get the result $query = $this->db->query('SELECT count(*),country,city,zip from country_location '); $res=$query->result_array(); echo $res[0]['count(*)'];
[eluser]cdbrkpnt[/eluser]
As bitist said you can try that or field alias would work out great ... $row = $this->db->select('count(*) as totalcount, name, address, telephone') $quantity = $row->totalcount;
[eluser]jmadsen[/eluser]
Another possibility (used for any calculated field): alias the field, and access it via the alias name i.e., Code: $row = $this->db->select('count(*) `my_count`, name, address, telephone')
[eluser]sandeep nami[/eluser]
@seanloving the result will be in the form of assosiative array and will be in the zero index Quote:echo $res[0]->totalcount Let me know if this helped
[eluser]Phil Sturgeon[/eluser]
[quote author="sandeep nami" date="1264164839"]You can use these functions and get the result $query = $this->db->query('SELECT count(*),country,city,zip from country_location '); $res=$query->result_array(); echo $res[0]['count(*)'];[/quote] What in the hell is that? Use row_array() instead of result, and give your count an alias. Or use the proper methods count_all_results(). http://ellislab.com/codeigniter/user-gui...ecord.html Code: echo $this->db->count_all_results('my_table'); Alternatively if you want to return information and the count, do your normal selecting and use $query->num_rows(); Code: $this->db->select('name, address, telephone'); Come on guys its all in the documentation.
[eluser]seanloving[/eluser]
The little trick of adding an alias is exactly I was looking for. As I am just learning how to make queries, I am grateful that the CI community is so willing to help with mundane questions. THANKS! -SL
[eluser]jmadsen[/eluser]
Uh, Phil... the number of rows returned is not the same as count(*) "SELECT count(*) `count` FROM table WHERE ..." ->num_rows() is 1 $row->count is how ever many rows meet the where clause... Maybe you want to read the post? ;-)
[eluser]Chad Fulton[/eluser]
[quote author="jmadsen" date="1264754428"]Uh, Phil... the number of rows returned is not the same as count(*) "SELECT count(*) `count` FROM table WHERE ..." ->num_rows() is 1 $row->count is how ever many rows meet the where clause... Maybe you want to read the post? ;-)[/quote] Maybe it's you who ought to read the post Although you make a good point in the case you talk about, if you read the first post, you'll notice that he's selecting the count(*) along with other variables, which means the count(*) number will be the same as the number of rows. |
Welcome Guest, Not a member yet? Register Sign In |