WordPress的数据库调用成员错误 [英] wordpress database call to a member error

查看:24
本文介绍了WordPress的数据库调用成员错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用wordpress中的自定义php将数据插入到我的数据库中.整个php文件包含以下内容:

I'm trying to insert data to my database using a custom php in wordpress. The entire php file contains this:

  <?php  global $wpdb;
    if(isset($_POST['Import']))
    {
     $filename = $_FILES["file"]["tmp_name"];
          if($_FILES["file"]["size"] > 0)
        {
        $file = fopen($filename, "r");


            $count = 0; 
            while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
            {
                $count++; 
                if($count>1){                                $wpdb->insert('wp_inventory_list',
                    array( 'project_code' => $emapData[0],
                           'phase' => $emapData[1],
                           'subphase' => $emapData[2],
                           'block' => $emapData[3],
                           'lot' => $emapData[4],
                           'lot_area' => $emapData[5],
                           'flr_area,' => $emapData[6],
                           'model_code' => $emapData[7],
                           'house_model' => $emapData[8],
                           'tcp' => $emapData[9] ),
                    array( '%s', '%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s'));
                }                  
            }
            fclose($file);
            echo 'CSV File has been successfully Imported';     
        }
        else
        {
            echo 'Invalid File:Please Upload CSV File';
        }

    }
?>

每当我尝试运行脚本时,都会出现错误在非对象上调用成员函数insert()".我的 $ wpdb 不能正常工作吗?我在 wp-content/themes/mytheme/文件夹中添加了自定义php文件.我还需要配置一些东西来使全局$ wpdb变量起作用吗?

Whenever I try to run the script I get an error "Call to a member function insert() on a non-object". Is my $wpdb not working right? I added the custom php file inside wp-content/themes/mytheme/ folder. Do I still need to configure something for the global $wpdb variable to work?

推荐答案

$ wpdb 可能为 null .您可以通过运行 var_dump($ wpdb)来解决此问题.如果未在wordpress框架中运行此脚本,则声明全局变量将无济于事.如果这是一个额外"文件,也就是说在您的情况下未在模板内部调用,则它将无法正常工作.

$wpdb is probably null. You can chack this by running var_dump($wpdb). Declaring the variable global won't help if this script is not run within the wordpress framework. If this is an "extra" file, meaning not called inside of the template in your case, it won't work like this.

在Wordpress 3.4+中,您必须这样做:

In Wordpress 3.4+ you have to do it like this:

define( 'SHORTINIT', true );
require_once('path/to/wp-load.php' );
global $wpdb;
...

对于较旧的Wordpress,请参见 http://www.stormyfrog.com/using-wpdb-outside-wordpress/.

For older Wordpress see http://www.stormyfrog.com/using-wpdb-outside-wordpress/.

这篇关于WordPress的数据库调用成员错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆