[RESOLVED] Trying to get property of non-object
Results 1 to 6 of 6

Thread: [RESOLVED] Trying to get property of non-object

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    5

    resolved [RESOLVED] Trying to get property of non-object

    This snippet of code is giving me a problem in an application I'm creating, and when I run it gives me the error above.

    Code:
    $mysqli = new mysqli('localhost', 'username', 'password', 'database');
    $ip = $_SERVER['REMOTE_ADDR'];
    $userinfo = array();
    	
    	$result = $mysqli->query('SELECT `ip` FROM `users` WHERE `ip` = ' .$ip. '');
    	if($mysqli->affected_rows == 0) {
    		return NULL;
    	}
    	
    	else {
    		return $userinfo = $result->fetch_array;
    	}
    Is there something wrong with my code; am I missing something? It would be nice to get an array of a user's information so it's easier to code different parts of my application. The full error message is:

    Notice: Trying to get property of non-object in test.php on line 12
    Last edited by Watakwa; 05-24-2008 at 03:54 PM.

  2. #2
    NMaOtBG bpat1434's Avatar
    Join Date
    Oct 2004
    Location
    Around 255.255.255.0
    Posts
    7,850
    mysqli->affected_rows deals with the result of an UPDATE, INSERT, CREATE statement. For SELECT statements, you want $result->num_rows

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    5
    Quote Originally Posted by bpat1434
    mysqli->affected_rows deals with the result of an UPDATE, INSERT, CREATE statement. For SELECT statements, you want $result->num_rows
    Should have been more precise with my post. I edited it and added more information.

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    5
    Quote Originally Posted by bpat1434
    mysqli->affected_rows deals with the result of an UPDATE, INSERT, CREATE statement. For SELECT statements, you want $result->num_rows
    Thank you. But that only made the problem jump from line 12 to line 7.
    Code:
    Line 7:
    	if($result->num_rows == 0) {
    Code:
    Line 12: 
    	return $userinfo = $result->fetch_array;
    Still giving me the Notice: Trying to get property of non-object error.

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,922
    Assume anything can fail, and check for (and handle) all errors:
    PHP Code:
    $mysqli = new mysqli('localhost''username''password''database');
    if(
    $mysqli === false)
    {
       
    user_error("Unable to connect to database.");
       return 
    false;
    }
    $query 'SELECT `ip` FROM `users` WHERE `ip` = ' $_SERVER['REMOTE_ADDR'];
    $result $mysqli->query($query);
    if(
    $result === false)
    {
       
    user_error("Query failed: ".mysqli->error."<br />\n$query");
       return 
    false;
    }
    if(
    $result->num_rows == 0)
    {
       return 
    null;
    }
    else
    {
       return 
    $result->fetch_array;

    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  6. #6
    Junior Member
    Join Date
    May 2008
    Posts
    5

    Talking

    Quote Originally Posted by NogDog
    Assume anything can fail, and check for (and handle) all errors: ......
    Thank you! That helped a lot, I guess I had a problem with my sql code. I really appreciate your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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