Jump to content

Fatal error: Call to a member function fetch_assoc() on a non-object in <file-path>


colin96

Recommended Posts

I've done a search on the error that I've received and following is one possible solution but I can see no reason why it would fail. I've tested my SQL prior to coding and the code works perfectly using xampp so it's related to the server.

 

"The error means what it says, what ever variable you are using in the $result->fetch_assoc() statement isn't an object because your query failed and returned a false value instead of a result object or you overwrote the variable somewhere else in your code."

 

I've tried it on two servers and whilst the following code works on xampp but fails on both servers. Another piece of code from which this was cut/pasted from works on xampp, works on one server but fails on the 2nd. I don't have any access to the configuration of the servers but have to reasonably assume it's in my code.

 

Could it possibly be because I'm using the $mysqli variable more than once? I would have thought it would simply overwrite it.

 

I've also put the top 3 lines of code at the top and run the different sql after branching with the same result.

 

Any thoughts on where to look next?!

 

Thanks,

Colin

 

//---------------------------------------------------------------------------------------------------------------------------------------------------

 

$mysqli = new mysqli("localhost", "login", "password", "database");
 
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

//---------------------------------------------------------------------------------------------------------------------------------------------------

 

if( $formField2 == 'd' ){
  $sql = "SELECT crewFirstname, crewSurname, count(*) as trips FROM crewmembers GROUP BY crewFirstname, crewSurname ORDER BY trips desc, crewSurname asc";
      } else {
        $sql = "SELECT crewFirstname, crewSurname, count(*) as trips FROM crewmembers GROUP BY crewFirstname, crewSurname ORDER BY crewSurname, crewFirstname asc";
}
     
if ( $formField1 > 0 ){
  $sql .= " LIMIT " . $formField1;
}
     
$mysqli->real_query( $sql );
$res = $mysqli->use_result();
$crew = array();
while ($row = $res->fetch_assoc()) {
  array_push( $crew, $row );
}
     

 

Link to comment
Share on other sites

Seems you missed half of that quote:

The error means what it says, what ever variable you are using in the $result->fetch_assoc() statement isn't an object because your query failed and returned a false value instead of a result object or you overwrote the variable somewhere else in your code.

Regardless of how well the code works on other servers, it doesn't work on one of them. So you should be trying to get error messages to find out why.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.