php 致命错误:在非对象上调用成员函数 get_results()(来自插件和 WordPress 的 jQuery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7521187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:51:15  来源:igfitidea点击:

Fatal error: Call to a member function get_results() on a non-object (jQuery From Plugin & WordPress)

phpselectwordpress-pluginjquery-forms-plugin

提问by Laxmidi

I'm trying to use jQuery's Form plugin with in a Wordpress plugin. I'm following this example. I've enqueued my scripts and built my form. In csf_form_handler.php, the equivalent of the example's json-echo.php, I can access the items selected in my form (I have a radiobutton group).

我正在尝试在 Wordpress 插件中使用 jQuery 的 Form 插件。我正在关注这个例子。我已经将我的脚本排入队列并构建了我的表单。在 csf_form_handler.php 中,相当于示例的 json-echo.php,我可以访问表单中选择的项目(我有一个单选按钮组)。

My goal is to use the values selected in the form in a SELECT statement to return data from a custom wordpress database table.

我的目标是在 SELECT 语句中使用表单中选择的值从自定义 wordpress 数据库表中返回数据。

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

global $wpdb;

$csf_db_table = $wpdb->prefix . "activity";


$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

Unfortunately, I'm getting:

不幸的是,我得到:

Notice: Trying to get property of non-object (on the $wpdb->prefix line)

Fatal error: Call to a member function get_results() on a non-object (on the $csf_data line)

注意:尝试获取非对象的属性(在 $wpdb->prefix 行上)

致命错误:调用非对象上的成员函数 get_results()(在 $csf_data 行上)

The code above in csf_form_handler.phpisn't in a function. I don't know if that makes a difference.

上面的代码 csf_form_handler.php不在函数中。我不知道这是否有区别。

How can I change the code so that I can use $wpdb?

如何更改代码以便我可以使用 $wpdb?

Thank you.

谢谢你。

回答by Umar Niazi

I came across the same issue but I got it resolved by including the wp-config.phpfile from the root of the WordPress folder like this:

我遇到了同样的问题,但我通过包含wp-config.phpWordPress 文件夹根目录中的文件解决了这个问题,如下所示:

require_once('../../../wp-config.php');
global $wpdb;

I hope this helps.

我希望这有帮助。

回答by Vidal Quevedo

When writing plugins, it's a good idea to keep your data-handling within the plugin's main file (i.e. not sending it to a separate file), and activate functions to handle it accordingly. Basically, you can set your form's action to point to either the plugin's file, or the page that contains the form.

编写插件时,最好将数据处理保留在插件的主文件中(即不要将其发送到单独的文件),并激活相应的函数来处理它。基本上,您可以将表单的操作设置为指向插件的文件或包含该表单的页面。

Let's assume this form you are working on is displayed on the front end of the site, on the sidebar. To handle data coming from that form when a user clicks “submit”, you could create a function in our plugin's file such as:

让我们假设您正在处理的这个表单显示在站点前端的侧边栏上。要在用户单击“提交”时处理来自该表单的数据,您可以在我们的插件文件中创建一个函数,例如:

function $csf_get_data(){

global $wpdb; //since your this function is in your plugin's file, $wpdb should be available, so no errors here! =)

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

$csf_db_table = $wpdb->prefix . "activity";

$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

    //do your stuff with $csf_data
}

//now run it everytime the plugin is run
if(isset($_POST[‘submit'])){
    $csf_get_data();
}

Now, you can set up your form action's property to send the data to the same page, which will be able to handle it using the function above. You could use either:

现在,您可以设置表单操作的属性以将数据发送到同一页面,该页面将能够使用上述函数进行处理。您可以使用:

    action=””

or

或者

    action="<?php the_permalink()?>"

Please note: to make sure the data is coming from your site (especially public forms), remember to use wp_nonce_field() to create a nonce field that can be validated byt wordpress via wp_nonce(): http://codex.wordpress.org/Function_Reference/wp_nonce_field

请注意:为确保数据来自您的站点(尤其是公共表单),请记住使用 wp_nonce_field() 创建一个 nonce 字段,该字段可由 wordpress 通过 wp_nonce() 进行验证:http://codex.wordpress.org /Function_Reference/wp_nonce_field

Hope that helps,

希望有所帮助,

Vq.

Vq。

回答by Vidal Quevedo

Where are you running this code? It sounds like you are running it BEFORE the $wpdb instance is created (or is out of scope).

你在哪里运行这段代码?听起来您是在创建 $wpdb 实例(或超出范围)之前运行它。

How are you loading your csf_form_handler.php file? Are you including as part of a script within Wordpress, or is it your own plugin? If it's the latter, remember that you need to activate it first, so WP can include it in the loading sequence (I'm assuming it's already activated, but JIC)

你如何加载你的 csf_form_handler.php 文件?您是将其作为脚本的一部分包含在 Wordpress 中,还是您自己的插件?如果是后者,记住需要先激活它,这样WP才能将它包含在加载序列中(我假设它已经激活了,但是JIC)

To see if the $wpdb object has already been created, you could run <?php print_r($wpdb);?>to print out the object's contents.

要查看 $wpdb 对象是否已经创建,您可以运行<?php print_r($wpdb);?>以打印出该对象的内容。

Let me know more, and hopefully I'd be able to help you.

让我知道更多,希望我能帮助你。

回答by aldobsom

I got this fatal error because I forgot to put

我得到了这个致命错误,因为我忘了把

global $wpdb;

inside the file where I put the $wpdb->get_results()function.

在我放置$wpdb->get_results()函数的文件中。