1

I'm creating an application in PHP for Wordpress and at this point, I'm trying to fetch a value from a database through AJAX every 2 seconds, and I've created a new file with the function that should be fired.

<?php
global $wpdb;
function conta(){
    global $wpdb, $table_name;
    $count = $wpdb->get_var("SELECT contatore FROM $table_name WHERE email = '$mail'");
    echo "$count";
}
conta()
?>

An this function is called here with an XMLHttpRequest Object every 2 seconds

    setInterval(function () {
    var url = <?php echo json_encode($con); ?>;
    var valuereq = new XMLHttpRequest();
    valuereq.open("GET", url, true);
    valuereq.send();
    valuereq.status;
    valuereq.onreadystatechange = function () {
        if (valuereq.readyState == 4 && valuereq.status == 200) {
            var return_data = valuereq.responseText;
            document.getElementById("conta").innerHTML = "Executed: " + return_data;
        }
        else document.getElementById("conta").innerHTML = "Error";
    }

}, 2000);

The Http request is executed correctly, but I receive this error:

Fatal error: Uncaught Error: Call to a member function get_var() on null in
C:\xampp\apps\wordpress\htdocs\wp-content\plugins\bittrex-
trader\admin\partials\conta.php:7 Stack trace: #0 
C:\xampp\apps\wordpress\htdocs\wp-content\plugins\bittrex-
trader\admin\partials\conta.php(11): conta() #1 {main} thrown in 
C:\xampp\apps\wordpress\htdocs\wp-content\plugins\bittrex-
trader\admin\partials\conta.php on line 7

The program is not able to find the global variable $wpdb, even though is declared. This happens only with the AJAX request because if i include directly the file $wpdb is found and working. Is there a reason? Thank you in advance.

Filippo
  • 97
  • 1
  • 11
  • have you included the file which the variable `$wpdb` had been set in ? – hassan Jan 01 '18 at 10:06
  • In function `conta()`, where did you declare or assign a value to variable `$mail`? – Ivan86 Jan 01 '18 at 10:07
  • $wpdb is a global variable of wordpress and it should be available inside wordpress plugins' files. I already used it many times in other files in the project. – Filippo Jan 01 '18 at 10:38
  • $mail is still not declared, but it is not the problem because it can't find $wpdb. I was thinking to manage that without help later. – Filippo Jan 01 '18 at 10:39

1 Answers1

1

Your $mail variable is not set inside the conta() function, yet you perform a query with it.

Use this to load basic wordpress (only loads wordpress core) so you can use $wpbd:

<?php

    // use this flag to load only wordpress core
    define( 'SHORTINIT', true );
    // find the wp-load.php file and require it
    require( '/path/to/wp-load.php' );

    // now you can use $wpdb


    function conta($wpdb, $table_name, $mail)
    {
       $count = $wpdb->get_var("SELECT `contatore` FROM `".$table_name."` WHERE `email` = '".$mail."'");
       echo "$count";
    }

    // call the function and pass the parameters
    conta($wpdb, $table_name, $mail);

    // close connection
    $wpdb->close();
?>

More on the flag for worpdress core here.

Ivan86
  • 5,695
  • 2
  • 14
  • 30