Avatar of bigjdve
bigjdve
 asked on

PHP Fatal error: Call to a member function fetch_assoc() on a non-object on line 19

This work with a die statement.  I put the die statement for testing.  But once I remove the die statement for it to continue processing, I get the php fatal error.  I tried to free the result set or close it, but it is still complaining.

See my code:

$mysqli = @new mysqli($db_host, $db_username, $db_password, $db_name);

$sql = "select * from table1";
$dataIndex = $mysqli->query($sql);

while ($result = $dataIndex->fetch_assoc())
{
      $field1 = $result['index_short_name']."_field1";
      $field2 = $result['index_short_name']."_field2";
      
      $sqlData = "select new_date, ".$field1.", ".$field2."  from table2";      
      $dataFund = $mysqli->query($sqlData);
      
      while ($row = $dataFund->fetch_assoc())
      {
            $line = "";
            $line .=  $row['new_date'].",".$row[$field1].",".$row[$field2]."\n";
            echo "<p>".$line."</p>";
            
      }
      die("here");
}

I replace the die("here") with $dataFund->close() and still error.
ProgrammingMySQL ServerPHP

Avatar of undefined
Last Comment
bigjdve

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dsmile

You should put error check to each query you have

$dataIndex = $mysqli->query($sql);
if (!$dataIndex ) {
   die('Invalid query: ' . mysql_error());
}


$dataFund = $mysqli->query($sqlData);
if (!$dataFund ) {
   die('Invalid query: ' . mysql_error());
}

I think one of those loops might meet empty result and it causes that error
bigjdve

ASKER
Basically, you can not do nested queries.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy