SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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
    PHP Code:
    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
    PHP Code:
        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

    PHP Code:
    $henCount $eggs->getHenCount(); // Get total number of hens
                
    $records $eggs->selectAllHens();
                include_once(
    'views/eggs_multiAdd.php'); 
    PHP Code:
    $records $eggs->selectAllHens() 
    works fine and the information is displayed in the view file just fine, but when i call
    PHP Code:
    $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 Code:
    <?php echo $henCount->total_hens;?>
    so i am not sure what's going on? Any insight?

    Thanks in advance!
    -Colin

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    - Robert

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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).

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,166
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cmarenburg View Post
    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
    Code:
    SELECT count(*) AS total_hens FROM `poultryinfo` WHERE `sex` = 'hen'

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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)
    - Robert

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,166
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    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.
    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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks @cpradio, and @QMonkey, I am not sure why i did fetch all. Just not thinking i guess lol. It's working now

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,166
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cmarenburg View Post
    Thanks @cpradio, and @QMonkey , I am not sure why i did fetch all. Just not thinking i guess lol. It's working now
    Doh!, Just caught on to what @QMonkey ; was pointing out, glad you got it on your first read @Cmarenburg ; Good catch QMonkey.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Also @cpradio i decieded to go with sizeof() that's a new to me and did not here of it untill now. Thanks you guys very help full as all ways

  10. #10
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I'm not sure that is the case...
    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.
    - Robert


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •