1

I have been trying for hours to simply call a table in a wordpress dbase through a .ajax post, and return the value I want, but I get this error on the returning page: "Call to a member function get_results() on a non object".

here is my php file:

<?php 
global $wpdb;


if (isset($_POST['state'])) {
    $results=$wpdb->get_results("SELECT DISTINCT make FROM cz_cars ORDER BY make ASC");
    echo $results[0][0];

exit;
}

?>

Here is the jquery i am using:


jQuery(document).ready(function() 
{

        jQuery("#caryear").change(function() 
        { 
            jQuery.ajax(
            { 
                type: "POST", 
                url: "wp-content/themes/storefront/includes/post-yearmakel.php",        
                data: ({ 'state' : jQuery("#caryear").val() }), 
                success: function(msg)
                {
                    jQuery('#carmake').append('' + msg + '');
                }
            });
        });

});

This code works fine when I comment out the database stuff in the php file, and just echo back a string. I have the php file with the other includes and it is being recognized by wordpress. No idea whats going on here...I thought having global $wpdb; would make this work!

Any ideas?

2 Answers 2

1

declaring something global is only meaningful outside of the global scope.

The first script doesn't see an initialized $wpdb. You need to include the WordPress engine or at least the part the sets up $wpdb

Sign up to request clarification or add additional context in comments.

3 Comments

Hi! thanks the reply! isnt there a place I can just save my php file so it has access to the wp global database? I changed my php so it has <?php include "../../../../wp-includes/wp-db.php"; $wpdb = new wpdb("root","******","pro","localhost");?> in it but now i get the error : use of undefined constant WP_DEBUG". I cant believe how frustrating this is!
np. Unfortunately WP isn't my specialty, but suffice it to say, that WP creates $wpdb somewhere in it's includes. If you aren't prepared to include wherever it's created (CMS's can be picky about how they permit including parts of themselves), you can just create your own one-off DB connection for the sake of this script.
In that case, I would recommend creating your own DB Connection: <a href="php.net/manual/en/function.mysql-connect.php">mysql_connect</a> <a href="php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a> <a href="php.net/manual/en/function.mysql-query.php">mysql_query</a>
0

I am not sure if the question has been answered or not but I believe you need to include the wp-load.php file.

It should be in the wordpress root install. It will give you access to wp globals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.