Welcome Guest, Not a member yet? Register   Sign In
Intermittent "call to a member fgunction result() on a non-object" errors...
#1

[eluser]GaryTheLlama[/eluser]
This error seems to happen at least once a day to each of my users.

Quote:Fatal error: Call to a member function result() on a non-object in M:\web\users\test\html\qual\SATCMS\_CodeIgniter\application\models\products_model.php on line 537

The line it's pointing to is pretty much always the "return $query->result();" line.

Code:
function GetNotes($parentId)
{
    $this->db->select("Note, DateCreated, EnteredBy");
    $this->db->where("ParentID", $parentId);
    $query = $this->db->get("tblNotes");
        
    return $query->result();
}

If they just resubmit the form, it works fine and will work 99% of the time. I'll be enabling the profile on a few users to see if I can collect some more information, but it seems like the database code is just having a hiccup.

Has anyone seen this? Any ideas or thoughts on how to prevent this or catch it and just re-try the code?
#2

[eluser]InsiteFX[/eluser]
You could try this:

Reconnecting / Keeping the Connection Alive

If the database server's idle timeout is exceeded while you're doing some heavy PHP lifting (processing an image, for instance), you should consider pinging the server by using the reconnect() method before sending further queries, which can gracefully keep the connection alive or re-establish it.
Code:
$this->db->reconnect();
#3

[eluser]GaryTheLlama[/eluser]
Thanks for the quick answer.

When and how should I properly use the reconnect method? Should I put it before every query? Or is there a way to check if the database server has timed out? I'm not really doing anything too PHP intensive.
#4

[eluser]GaryTheLlama[/eluser]
I should mention that I'm using SQL Server. Doesn't seem I can use the reconnect method.

Code:
Call to undefined method CI_DB_sqlsrv_driver::reconnect()
#5

[eluser]aquary[/eluser]
You could try logging the $query before running the result() part, to see what is the type of the $query. Then you could start tracing how could it be in that type after.

To prevent the error, you "could" put a method_exists() for checking if the method result() is available in $query.... but seems to be overkill here.

Also, I've checked about the reconnect() in sqlsrv driver. You cannot use it since it's not implemented, neither in the code, nor the SQL server itself.
Code:
// --------------------------------------------------------------------

function reconnect()
{
  // not implemented in MSSQL
}

But you shouldn't get that kind of error anyway....
#6

[eluser]Chathuranga Tennakoon[/eluser]
the below code might work for you!
Code:
function GetNotes($parentId)
{

  $CI =& get_instance();

  $CI->db->select(“Note, DateCreated, EnteredBy”);
  $CI->db->where(“ParentID”, $parentId);
  $query = $CI->db->get(“tblNotes”);
    
  return $query->result();
}


#7

[eluser]GaryTheLlama[/eluser]
Interesting. What's the difference between calling $this and using the $CI super object in your model? I know you can use the super object to reference the database externally (like in the controller), but how does that help me since my code is already in the model?
#8

[eluser]Aken[/eluser]
Using the instance method is unnecessary for models unless you don't want to extend CI_Model. Don't listen to them.

It seems, like you said, that the database is hiccuping somewhere. It likely has something to do with the connection between the database and CI. Since it's intermittent, it's really hard to narrow down, and I doubt it's a code problem. If you have a server admin (like your hosting company, for instance), perhaps contacting them and asking if they can help narrow something down through logs or something may help.

In the meantime, you can add some logic to your model that checks if $query is available to provide a result() so that it doesn't throw a fatal error to the user.
#9

[eluser]CroNiX[/eluser]
If they are enabled (default), try turning persistent connections off in the db config.
#10

[eluser]GaryTheLlama[/eluser]
I've been having it spit out the last query when the error happens and it seems to be an issue with either the post variables or session variables being null. I don't believe it's time out related, since users claim to be continually using the site and not letting it sit. Any thoughts on what could cause any of those variables to die or be lost?




Theme © iAndrew 2016 - Forum software by © MyBB