how to access the count(*) property? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: how to access the count(*) property? (/showthread.php?tid=26744) Pages:
1
2
|
how to access the count(*) property? - El Forum - 01-21-2010 [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 how to access the count(*) property? - El Forum - 01-22-2010 [eluser]Unknown[/eluser] Instead of: Code: $quantity = $row->count(*); Try to use this form: Code: $quantity = $this->db->count_all_results(); how to access the count(*) property? - El Forum - 01-22-2010 [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(*)']; how to access the count(*) property? - El Forum - 01-22-2010 [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; how to access the count(*) property? - El Forum - 01-22-2010 [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') how to access the count(*) property? - El Forum - 01-22-2010 [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 how to access the count(*) property? - El Forum - 01-22-2010 [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-guide/database/active_record.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. how to access the count(*) property? - El Forum - 01-22-2010 [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 how to access the count(*) property? - El Forum - 01-28-2010 [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? ;-) how to access the count(*) property? - El Forum - 01-28-2010 [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. |