Forum Moderators: coopster

Message Too Old, No Replies

Non-Object Error

Trying to get property of non-object

         

calmseas

3:55 am on Sep 10, 2010 (gmt 0)

10+ Year Member



In the following code snippet I get the error: "Notice: Trying to get property of non-object". Is not $item an object?

$query = "SELECT no_of_logons FROM users WHERE userid= '$userid'
AND password = '$password'" ;

$result = mysqli_query($db , $query ) ;
if ( $result ) {
$item = $result -> fetch_object() ;
$item_count = $item -> no_of_logons ;
}

Matthew1980

7:50 am on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there CalmSeas,

$query = "SELECT `no_of_logons` FROM `users` WHERE `userid` = '".$userid."' AND `password` = '".$password."'";

$result = mysqli_query($db,$query);

if ($result){

while($item = $result->fetch_object($result)){
echo "<pre>";
print_r($item -> no_of_logons);
echo "</pre>";

}
else{
echo "Query Unsuccessful";
exit;
}


This is more or less what you want, for debugging purposes, just echo the returned array first to see that the results are as you expect, once you can see that they are you can then carry on from there.

Hope that helps,

Cheers,
MRb

penders

4:02 pm on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



while ($item = $result->fetch_object($result)) {


Do not pass $result to the fetch_object() method, unless using the mysqli_fetch_object() procedural style call.

calmseas

5:00 pm on Sep 10, 2010 (gmt 0)

10+ Year Member



Matthew -
Thank you for your suggestions as I was unaware of the print_r() function. I was wondering how to display the results of the returned array.

Penders -
Excellent.........that did the trick. May I ask if it is better to concentrate on learning the object oriented approach or the procedural style, or is it a matter of personal preference or does it depend on circumstances. I'm making mistakes because I get confused by the syntax wrt the two styles.

penders

10:23 pm on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...is better to concentrate on learning the object oriented approach or the procedural style


Stick to the OO approach IMO. One of the advantages of the mysqli extension over the older MySQL extension is it's OO interface - easier to maintain, extendable, etc. I see no benefit of using the procedural interface for new projects.

The only possible reason I can think of for using the procedural interface is if you were doing a quick update of a current project from MySQL to mysqli - may be?

calmseas

4:10 pm on Sep 11, 2010 (gmt 0)

10+ Year Member



Penders -

Thank you for your recommendation, which I'm going to take. I'm converting an app from Java Server Pages, so I'm in the middle of all this new stuff (to me ).

Matthew1980

10:07 pm on Sep 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Do not pass $result to the fetch_object() method, unless using the mysqli_fetch_object() procedural style call


Oop's, my mistake I hadn't read the post properly, my bad ;p

OOp is certainly a great thing to learn, I seem to have learnt things the wrong way around, I wish that I had learned OOp first when I started doing websites 10 yesrs ago, but I wasn't too aware of that, and being from a C/Java/assembly language background I wasn't too fussed when I did find out about it; having said that, hind sight is a great tool! I am now learning OOp and find it so useful to use, and really makes sense, and improves your coding style I think, plainly because it makes you think about how you structure things and the logic that you use.

So if your starting out, do OOp, it's worth the headbanging and initial confusion, but it will pay huge dividends in the future; good luck & above all have fun doing it.

Cheers,
MRb