duanbei1709 2019-08-11 13:58
浏览 252

为什么我不能在wordpress中使用$ wpdb

I am trying to make ajax in wordpress.

The php file is placed in plugins folder as a plugin

wp-content/ plugins/ my_plugin/ php-file.php 

I know that Ajax receives informations from the php file because I did some tests, so the url in ajax is correct !

But when I want to pull some data from the database, I get this error in log file:

PHP Fatal error: Uncaught Error: Call to a member function get_results() on null in /home/......

I specify that I have an error 500 in the browser console (network tab)

my code :

global $wpdb;
$table_name = $wpdb->prefix . "wplusersprofiles";
$user = $wpdb->get_results('SELECT * FROM my_table');
$live_Str = $user[0]->etat;   

the problem is therefore is on line 3

  • 写回答

1条回答 默认 最新

  • dpicx06888 2019-08-11 18:27
    关注

    Thank you jeff, Yes the 2nd line was not necessary, but according the log the probleme is in 3rd line ...

    In fact This code return me data, i can see it in the top of the page , But this data can not be sent via ajax, So i have always error that triggers instead of success :(

    This is my js code :

    $.ajax({
         url: "https://my-web-site.com/lab1/wp-content/plugins/my-plugin/my-plugin.php",
         type: "POST",
         data: {etat:"verif"},
           success: function(data) {
           
           if (data=='on')
           {
                 activate_livestream(); // this is a simple js function
           }
           else {
             console.log('off');
           }
    
         },
         error:function(){
           alert('ERROOOOR'); // i get always this alert :( !!
         }
       })// $.ajax end 


    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?