0

Hi I have an error that it's: Call to a member function insert() on null that the function $wpdb->insert(); returns.

Now, this is my array: 

Array ( 
  [title] => Huqvarna 610 cross blocco motore 
  [description] => Vendo blocco motore husqvarna 610 cross tenuto di scorta sempre in magazzino usato pochissimo perfetto 
  [locale] => Riccione 
  [timestamp] => Oggi, 19:15 
  [price] => 500 € 
  [ad_type] => 1 
)

and this is my code:

<?php 
class The_Crawler { 

    public function __construct() { } 
    public static function get_classifieds( $urls ) 
    { 
        global $wpdb; 
        $dom_document = new DOMDocument(); 
        foreach($urls as $url) 
        { 
            if( ! function_exists( 'wp_remote_request' ) ) 
            { 
                $html = file_get_contents( $url ); 
                $dom_document->loadHTML( $html ); 
            } else { 
                $response = wp_remote_request( $url ); 
                $body = wp_remote_retrieve_body( $response ); 
                $dom_document->loadHTML( $body ); 
            } 
            $li_elements = $dom_document->getElementsByTagName( 'li' ); 
            foreach( $li_elements as $li_element ) 
            {
                $pattern = '/item\sresult/'; 
                $class = $li_element->getAttribute( 'class' ); 
                if( preg_match( $pattern, $class ) ) 
                { 
                    $classified = []; 
                    $img_elements = $li_element->getElementsByTagName( 'img' ); 
                    $h3_elements = $li_element->getElementsByTagName( 'h3' ); 
                    $h4_elements = $li_element->getElementsByTagName( 'h4' ); 
                    $p_elements = $li_element->getElementsByTagName( 'p' ); 

                    foreach( $h3_elements as $h3_element) 
                    { 
                        $h3_class = $h3_element->getAttribute( 'class' ); 
                        if( $h3_class == 'title' ) 
                        { 
                            $classified['title'] = $h3_element->nodeValue; 
                            // echo "<h3>$h3_element->nodeValue</h3>"; 
                        } 
                    } 

                    foreach( $img_elements as $img_element) {
                        $src = $img_element->getAttribute( 'src' ); 
                        if( strpos( $src, 'img' ) && ! strpos( $src, 'no-img' ) ) 
                        { 
                            $classified['src'] = $src; 
                            // echo "<img src='$src' />";
                        } 
                    } 
                    foreach( $p_elements as $p_element) { 
                        $p_class = $p_element->getAttribute( 'class' ); 
                        if( $p_class == 'description' ) { 
                            $classified['description'] = $p_element->nodeValue;
                            // echo "<p>$p_element->nodeValue</p>"; 
                        } elseif( $p_class == 'locale' ) { 
                            $classified['locale'] = $p_element->nodeValue; 
                            // echo "<p><strong>$p_element->nodeValue</strong></p>"; 
                        } elseif( $p_class == 'timestamp' ) { 
                            $classified['timestamp'] = $p_element->nodeValue; 
                            // echo "<p>$p_element->nodeValue</p>"; 
                        } 
                    } 
                    foreach( $h4_elements as $h4_element) { 
                        $h4_class = $h4_element->getAttribute( 'class' ); 
                        if( $h4_class == 'price' ) { 
                            $classified['price'] = $h4_element->nodeValue; 
                            // echo "<h4>$h4_element->nodeValue</h4>"; 
                        } 
                    } 
                    $classified['ad_type'] = 1; 
                    print_r($classified); 
                    $query = $wpdb->insert( 'wp_classifieds', $classified );
                    echo $query; 
                } 
            } 
        } 
    } 
} 
$urls = [ 
    'https://www.kijiji.it/motori/moto-e-scooter/', 
    'https://www.kijiji.it/elettronica/' 
]; 
The_Crawler::get_classifieds( $urls );

The query passed to insert function isn’t null how can you see in the array result above, but the error says that a member passed to the function is null: why? Didn’t I understand something?

Update: I used 

var_dump()

 function on 

$wpdb

 global variable and it returns null. Why?

  • 2
    Where are you running this code? – Jacob Peattie May 19 at 11:11
  • @JacobPeattie this code is running in a custom plugin. Precisely, in a plugin based on Devin Vinson boilerplate. This is a custom file into the boilerplate: the same $wpdb into the initial files isn't null instead in this custom file is null also if it's on the custom plugin. – Salvatore Riccardi May 19 at 13:18
  • I don't think that's right. $wpdb is available very early. WordPress wouldn't be able to determine active plugins without it. So it should be available that early. – Jacob Peattie May 20 at 3:02
  • Ok so what's the right answer? – Salvatore Riccardi May 20 at 5:15
  • @JacobPeattie you're right, WordPress has been performing database queries before. – nmr May 20 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.