Welcome Guest, Not a member yet? Register   Sign In
how to access the count(*) property?
#1

[eluser]seanloving[/eluser]
One of my database queries starts like this:

Code:
$row = $this->db->select('count(*), name, address, telephone')
then later on I when I access the object properties like this...
Code:
$name = $row->name;
$address = $row->address;
$phone = $row->telephone;

$quantity = $row->count(*);  // gives error
I get an error at the count(*) property.

What is the proper syntax to access the count(*) property?

Thanks
Sean Loving
#2

[eluser]Unknown[/eluser]
Instead of:
Code:
$quantity = $row->count(*);

Try to use this form:
Code:
$quantity = $this->db->count_all_results();
#3

[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(*)'];
#4

[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;


Smile
#5

[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')
$quantity = $row->my_count;
#6

[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
#7

[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');
// Produces an integer, like 25

$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results();
// Produces an integer, like 17

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');
$query = $this->db->get('table_name');

$row = $query->row()

$name = $row->name;
$address = $row->address;
$phone = $row->telephone;

$quantity = $query->num_rows();

Come on guys its all in the documentation.
#8

[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
#9

[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? ;-)
#10

[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 Wink

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.




Theme © iAndrew 2016 - Forum software by © MyBB