开发者

Working with custom table in wordpress database

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",        
                dat开发者_JAVA百科a: ({ '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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜