10 Replies - 507 Views - Last Post: 22 March 2013 - 02:07 AM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Call to a member function on a non object.

Posted 21 March 2013 - 03:57 PM

I've seen this error before, but it's always been because of a typo in my query. I'm getting it with this function.
  function GetNavigation()
    {
    	global $con;
    	$qry = "SELECT link, name FROM navigation WHERE enabled='1'";
    	
    	$result = mysqli_query($con,$qry);
    	
    	while($row = mysqli_fetch_array($result))
    	{
    	echo '<a href="'.$row['link'].'">'.$row['name'].'</a>';
    	}
    }



I've used the same format on other functions and they've thrown no issues. Does anybody have any clue why I'd be receiving this on this one function?

Is This A Good Question/Topic? 0
  • +

Replies To: Call to a member function on a non object.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,413
  • Joined: 08-June 10

Re: Call to a member function on a non object.

Posted 21 March 2013 - 11:26 PM

the problem is that you donít test, whether your query is successful (i.e. returns a MySQLi_Result object).

i wonder if "name" is a MySQL reserved keyword ...
Was This Post Helpful? 0
  • +
  • -

#3 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Call to a member function on a non object.

Posted 22 March 2013 - 12:51 AM

  function GetNavigation()
  {
  	global $con;
  	$qry = "SELECT link, name FROM navigation WHERE enabled='1'";
  	$mysqliDebug = true;
  	$result = mysqli_query($con,$qry);
 if (!$result) {
 if ($mysqliDebug) {
 echo mysqli_errno;
 }
    die();
    }
 
  	while($row = mysqli_fetch_array($result))
  	{
  	echo '<a href="'.$row['link'].'">'.$row['name'].'</a>';
  	}
  }


I added that in the hopes I could get more information. It's giving the exact same error. =/
Was This Post Helpful? 0
  • +
  • -

#4 GrooN  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 27-February 13

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:11 AM

you don't call mysqli_errno, you just echo it.
Besides, have you checked that your $con variable actually has a connection, you can check it with:
echo @mysql_ping() ? 'true' : 'false';

This post has been edited by Dormilich: 22 March 2013 - 01:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:15 AM

I also have a similar function for checking mail.
    function CheckMail()
    {
  
               
        $id = $_SESSION['id'];
          global $con;
        $qry = "Select * from mail where recieveid=$id and unread=1";
        
        $result = mysqli_query($con,$qry);
        while($row = mysqli_fetch_array($result))
        {
        
	if(empty($row))
        {
        echo 'No mail';
            return true;
        }
        else
        	{
                	echo 'working';
                	return true;

        	}
        }
    }


I place this in my content div and it works just fine. The instant I try to transfer it to my navigation div though it starts returning the same error.

View PostGrooN, on 22 March 2013 - 01:11 AM, said:

you don't call mysqli_errno, you just echo it.
Besides, have you checked that your $con variable actually has a connection, you can check it with:
echo @mysql_ping() ? 'true' : 'false';


Yeah; I've ran them through a seperate section on the page and they're actually working. Now I'm more lost then before as to why.

This post has been edited by Dormilich: 22 March 2013 - 01:26 AM

Was This Post Helpful? 0
  • +
  • -

#6 GrooN  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 27-February 13

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:18 AM

Okay, I don't know how to edit posts do I am writing a new one.

You should check your variables, and see whether they're objects. If you don't have any debugging tools installed, just var_dump them. Also it would be a good idea to check at which line the error actually occurs. Maybe it's not even from this function. If $con returns null somethings wrong with your connection :)
Was This Post Helpful? 0
  • +
  • -

#7 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:22 AM

That's where I'm drawing a blank. Everything works fine inside div class content. I move them into my div class navigation to implement them and they're done. Suddenly errors going through.
Was This Post Helpful? 0
  • +
  • -

#8 GrooN  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 27-February 13

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:29 AM

There's nothing wrong with the code you posted. Gonna be something wrong with your database connection. Have you tried making the connection directly in the function? Like this:

$con=mysqli_connect("localhost","root","pass","test");

// Check connection
if (mysqli_connect_errno($con))
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$qry = "SELECT link, name FROM navigation WHERE enabled='1'";
$mysqliDebug = true;
$result = mysqli_query($con,$qry);

if (!$result) {
    if ($mysqliDebug) {
      echo mysqli_errno();
    }
    die();
}

while($row = mysqli_fetch_array($result))
{
    echo '<a href="'.$row['link'].'">'.$row['name'].'</a><br />';
}


Was This Post Helpful? 0
  • +
  • -

#9 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:43 AM

I can get it to work by placing the code directly inside the nav include file. Not the way I wanted to go but I suppose it's going to have to work until I stop nubbing up my coding somewhere.
Was This Post Helpful? 0
  • +
  • -

#10 GrooN  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 27-February 13

Re: Call to a member function on a non object.

Posted 22 March 2013 - 01:46 AM

I don't know what's wrong with my spelling in my previous posts. Must've been in too much of a hurry xD. Anyway, glad to hear it works, we've now know that it was the $con variable that was the issue. You probably have forgotten to include the connection part on the page, who knows. Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#11 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Call to a member function on a non object.

Posted 22 March 2013 - 02:07 AM

lol thanks, I wish I knew that something as simple as $con was the issue, but I have it being used a multitude of times. I placed the entire function as a statement on the panel I use for my navigation. Hopefully soon I can get it fully sorted out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1