PHP Fatal error: Uncaught Error: Call to a member function query() on null
-
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>"; } } } } } }
- The topic ‘PHP Fatal error: Uncaught Error: Call to a member function query() on null’ is closed to new replies.