简体   繁体   English

在Wordpress数据库中使用自定义表

[英]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". 我已经尝试了几个小时,只是通过.ajax帖子在wordpress dbase中调用一个表,并返回我想要的值,但是我在返回的页面上收到此错误:“在非函数中调用成员函数get_results()宾语”。

here is my php file: 这是我的php文件:

<?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:


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. 当我注释掉php文件中的数据库内容并回显字符串时,此代码可以正常工作。 I have the php file with the other includes and it is being recognized by wordpress. 我有包含其他文件的php文件,并且被wordpress识别。 No idea whats going on here...I thought having global $wpdb; 不知道这是怎么回事...我以为拥有全局$ wpdb; would make this work! 将使这项工作!

Any ideas? 有任何想法吗?

declaring something global is only meaningful outside of the global scope. 声明global仅在global范围之外有意义。

The first script doesn't see an initialized $wpdb . 第一个脚本没有看到初始化的$wpdb You need to include the WordPress engine or at least the part the sets up $wpdb 您需要include WordPress引擎或至少包含设置$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. 我不确定问题是否已经回答,但我相信您需要包括wp-load.php文件。

It should be in the wordpress root install. 它应该在wordpress root安装中。 It will give you access to wp globals. 它将使您能够访问wp globals。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM