Property of non obeject error. Can't figure out why

Hey guys and gals,

I am getting a property of non object and i can not figure out why.

Here is the function that does not seem to be working

public function getHenCount(){
		$db = new dboptions();
		$record = $db->rawSelect("SELECT count(*) FROM `poultryinfo` AS total_hens WHERE `sex` = 'hen'");
		return $record->fetchAll(PDO::FETCH_OBJ);
	}

but this one does, and i have tested out the above sql query so i know it’s not that. So than it must be how i define the accesor

	public function selectAllHens(){
		$db = new dboptions();
		$record = $db->rawSelect("SELECT * FROM poultryinfo WHERE `sex` = 'hen'");
		return $record->fetchAll(PDO::FETCH_OBJ);
	}

and here is how i do that


$henCount = $eggs->getHenCount(); // Get total number of hens
			$records = $eggs->selectAllHens();
			include_once('views/eggs_multiAdd.php');

$records = $eggs->selectAllHens()

works fine and the information is displayed in the view file just fine, but when i call

$henCount->total_hens

in the view file i get Trying to get property of non-object in eggs_multiAdd.php on line 33 and that is just

<?php echo $henCount->total_hens;?>

so i am not sure what’s going on? Any insight?

Thanks in advance!
-Colin

Do something like
var_dump( $henCount );

before your $henCount->total_hens; and see what you actually have there. Apparently it’s not an object. Likely NULL.

So this is what it returns
array(1) { [0]=> object(stdClass)#5 (1) { [“count(*)”]=> string(1) “2” } } so the query is being run, and i can see the number that should be there (2).

Try changing your query to

SELECT count(*) AS total_hens FROM `poultryinfo` WHERE `sex` = 'hen'

Oh, I didn’t look closely enough before replying. fetchAll() returns an array. So you have there an array of objects. Since you’re only expecting one result from that query, you may want just fetch() instead.

(along with what cpradio said above. the query does need to be changed in order to use ->total_hens as you do)

I’m not sure that is the case, as why would you perform count(*) on the same table with the same WHERE, getting a result of 2, if you expected one row returned?

Another way to look at this though, is since you are performing a fetchAll() you could just perform sizeof($records) to get the number of rows returned and not need a second query.

Thanks @cpradio, and @QMonkey, I am not sure why i did fetch all. Just not thinking i guess lol. It’s working now :slight_smile:

Doh!, Just caught on to what @QMonkey ; was pointing out, glad you got it on your first read @Cmarenburg ; :slight_smile: Good catch QMonkey.

Also @cpradio i decieded to go with sizeof() that’s a new to me and did not here of it untill now. :slight_smile: Thanks you guys very help full as all ways :slight_smile:

I was referring to the query in getHenCount(). SELECT COUNT(*) will always return one row (unless you GROUP BY) not that the count will always be one. But you’re right if Colin wants to optimize, get the count from selectAllHens() and you don’t need the other at all.