Home  >  Q&A  >  body text

Fatal error: Uncaught error: Calling member function find() on bool in C Stack trace: #0 {main} thrown in C at line 16

I am getting this error above and my code is below. Is there anything wrong with my code? Any help is appreciated. I'm not sure if it has to do with the software I'm using to run it, XAMPP Apache. I'm currently trying to retrieve information from a web page that contains product codes in Excel. I managed to run around 900+ production codes but suddenly I got the above error.

<?php
// example of how to use basic selector to retrieve HTML contents
include('simple_html_dom.php');

$file = fopen("Book1.csv","r");
$file2 = fopen("test.csv","w");
$links = [];

while (($result = fgetcsv($file)) !== false)
{
    $link = "https://mall/Product/".$result[0];
    $links[] = $link;

    $row_data = [];
    $page = file_get_html($link);
    $product_details = $page->find('.ProductDetailsTable tr'); //line 16
    if(count($product_details)==0) {
        $row_data[] = $result[0];
        $row_data[] = 'not found'; 
        fputcsv($file2, $row_data);
        continue;
    }

    //second method
    $article_number = '';
    $product_description = '';
    $product_family = '';
    $product_lifecycle = '';
    $plm_date = '';
    $notes = '';
    $EAN = '';
    $UPC = '';
    $country_of_origin = '';

    foreach($product_details as $table_row) {    
        if(count($table_row->find('td'))==1){
            //ignore
        } elseif(count($table_row->find('td'))==2) {
            $key = $table_row->find('td')[0]->plaintext;
            $value = $table_row->find('td')[1]->plaintext;
            if($key=="EAN") {
                $EAN = $value;
            }
            elseif($key=='Article Number (Market Facing Number)') {
                $article_number = $value;
            } elseif ($key=='Product Description') {
                $product_description = $value;
            } elseif ($key=='Product family') {
                $product_family = $value;
            }elseif ($key=='Product Lifecycle (PLM)') {
                $product_lifecycle = $value;
            }elseif ($key=='PLM Effective Date') {
                $plm_date = $value;
            }elseif ($key=='Notes') {
                $notes = $value;
            }elseif ($key=='UPC') {
                $UPC = $value;
            }elseif ($key=='Country of origin') {
                $country_of_origin = $value;
            }    
        } 
    }
    $row_data[] = trim($article_number);
    $row_data[] = trim($product_description);
    $row_data[] = trim($product_family);
    $row_data[] = trim($product_lifecycle);
    $row_data[] = trim($plm_date);
    $row_data[] = trim($notes);
    $row_data[] = trim($EAN);
    $row_data[] = trim($UPC);
    $row_data[] = trim($country_of_origin);
    fputcsv($file2, $row_data);
}

fclose($file);
fclose($file2);
echo 'done';

?>

P粉878510551P粉878510551270 days ago374

reply all(1)I'll reply

  • P粉237647645

    P粉2376476452023-12-29 23:12:44

    This happens because file_get_html() returns a boolean value (which may be false due to some error, such as an invalid url).

    Your code does not check for failure.

    I suggest you add the following:

    if (!$page) {
      // Do some error handling, logging
      continue; // skip to next iteration on your loop.
    }

    reply
    0
  • Cancelreply