• Why $wpdb global returns null in this code? I can’t be able to do the mysql query because the global is null. Below the error in the error log file:

    PHP Fatal error: Uncaught Error: Call to a member function query() on null

    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 ) ) {
    
                        $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' );
                            $is_null = $wpdb->query( "SELECT IFNULL( SELECT * FROM '{$wpdb->prefix}classifieds' WHERE title = {$h3_element->nodeValue})" , true );      
    
                            if( $h3_class == 'title' && $is_null ) {
    
                                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' ) ) {
    
                                echo "<img src='$src' />";
    
                            }
    
                        }
    
                        foreach( $p_elements as $p_element) {
    
                            $p_class = $p_element->getAttribute( 'class' );
    
                            if( $p_class == 'description' ) {
    
                                echo "<p>$p_element->nodeValue</p>";
    
                            } elseif( $p_class == 'locale' ) {
    
                                echo "<p><strong>$p_element->nodeValue</strong></p>";
    
                            } elseif( $p_class == 'timestamp' ) {
    
                                echo "<p>$p_element->nodeValue</p>";
    
                            }
    
                        }
    
                        foreach( $h4_elements as $h4_element) {
    
                            $h4_class = $h4_element->getAttribute( 'class' );
    
                            if( $h4_class == 'price' ) {
    
                                echo "<h4>$h4_element->nodeValue</h4>";
    
                            }
    
                        }
    
                    }
    
                }
    
            }
    
        }
Viewing 5 replies - 16 through 20 (of 20 total)
  • Thread Starter salvatorericcardi

    (@richdeveloper)

    Ok, I repeat myself until we get to the solution: above there is the code that it must call $wpdb global variable to do a query into the database. I can do it also with PHP function but if I’m using WordPress I want to use it for coding. Now, if I call the $wpdb variable like global into my custom class of the boilerplate plugin, PHP functions work but not the same of WordPress because it isn’t loaded by the plugin. So, how can the plugin load WordPress core? Just so I can use the WordPress function and in another solution.

    Finally, if you give me an email I can share to you my plugin code so it’s easier. Thanks ??

    We are volunteers, supporting WordPress here in these forums, not reading your code in email.

    If you are calling this code a WordPress plugin, WordPress should be loading the plugin, not the plugin loading WordPress. It should already be in the WordPress environment. If it’s not, it’s not a WordPress plugin. In order for the code to execute at all, it has to be called somehow. Where is it called? Is it anything in WordPress that triggers the code to execute? If not, figure out where that should be and add it. Then you will have your $wpdb variable defined.

    Thread Starter salvatorericcardi

    (@richdeveloper)

    I’m sorry I did a lot of confusion, the error is: 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?

    Thread Starter salvatorericcardi

    (@richdeveloper)

    Update: I used var_dump() function on $wpdb global variable and it returns null. Why?

    Again, I can’t see when and where your code is invoked. That’s on you to figure out.

    At this point, we’ve reached the limits of what most in the community here are willing to offer in their spare time (everyone here is a volunteer).

    You might get more developer eyes at https://wordpress.stackexchange.com/

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘PHP Fatal error: Uncaught Error: Call to a member function query() on null’ is closed to new replies.