CodeIgniter错误:尝试获取非对象数据库的属性

时间:2015-05-06 14:17:02

标签: php codeigniter

我有一个带tecdoc数据库的汽车零件网站,我通过他们的api包含价格和库存的提供商。

输入产品页面时出错

  

遇到PHP错误严重性:通知消息:试图获取   非对象的属性文件名:controllers / tree.php行号:349

这是349上的一行:

$this->db->where('PRI_ART_ID',$idarticolx->ART_ID);

这是代码:

        require_once APPPATH.'third_party/AutoNet.php';//$this->load->library('AutoNet');
        $autonet_api = get_rest_autonet($params);
        $result = json_decode($autonet_api,true);
        // echo '<pre>';
        // die(print_r($result));
        if ($result['Error']['HasError'] == false) {

            $articles=$result['ArticleOffers'];

            if (!isset($articles['Article'])) { // daca sunt mai multe rezultate
                for ($n=0;$n<count($articles);$n++){
                    if  ($articles[$n]['StockStateText'] != ''){
                        $this->db->select('A.*');
                        $this->db->select('B.adaos');
                        $this->db->from('tof_articles AS A');
                        $this->db->join('list_suppliers AS B','B.SUP_ID=A.ART_SUP_ID');
                        $this->db->where('A.AUG_NR',$articles[$n]['PartNo']);
                        $this->db->where('A.ART_SUP_ID',$articles[$n]['TDBrandId']);
                        $idarticolx=$this->db->get()->row();
                        // $this->output->enable_profiler(TRUE);
                        // echo "select A.*, B.adaos form tof_articles AS A join list_suppliers AS B, B.SUP_ID=A.ART_SUP_ID where A.ART_ARTICLE_NR = '{$articles[$n]['PartNo']}' AND A.ART_SUP_ID '{$articles[$n]['TDBrandId']}'";
                        // echo "<pre>";
                        // die(print_r($idarticolx));
                        $this->db->select('*');
                        $this->db->from('list_prices AS A');
                        $this->db->where('PRI_ART_ID',$row['id_articol']);
                        $b=$this->db->get();
                        if ($b->num_rows() > 0) {
                            $this->db->from('list_prices');
                            $this->db->set('PRI_PRICE_AC');
                            $this->db->set('PRI_PRICE');
                            $this->db->set('id_furnizor', '43');
                            $this->db->set('last_update', time());
                            $this->db->set('ST_1', '1');
                            $this->db->where('PRI_ART_ID',$idarticolx->ART_ID);//this is line 349
                            $this->db->update();
                        } else {
                            $this->db->from('list_prices');
                            $this->db->set('PRI_ART_ID' , $row['id_articol']);
                            $this->db->set('PRI_PRICE_AC', $articles[$n]['PriceWoVat']);
                            $this->db->set('PRI_PRICE', ceil($articles[$n]['PriceWoVat']));
                            $this->db->set('id_furnizor', '43');
                            $this->db->set('last_update', time());
                            $this->db->set('ST_1', '1');

                            $this->db->insert();
                        }


                    }

                }

            } else { // daca este un singur rezultat

                if  ($articles['Description'] != ''){

                    $this->db->select('A.*');

                    $this->db->select('B.adaos');

                    $this->db->from('tof_articles AS A');

                    $this->db->join('list_suppliers AS B','B.SUP_ID=A.ART_SUP_ID');

                    $this->db->where('A.ART_ARTICLE_NR',$articles['Article']);

                    $this->db->where('A.ART_SUP_ID',$articles['Brand']);

                    $idarticolx=$this->db->get()->row();



                    $this->db->select('*');

                    $this->db->from('list_prices AS A');

                    $this->db->where('A.PRI_ART_ID',$idarticolx->ART_ID);

                    $b=$this->db->get();

                    if ($b->num_rows() > 0) {

                        $this->db->from('list_prices');

                        $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);

                        $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));

                        $this->db->set('id_furnizor', '2');

                        $this->db->set('last_update', time());

                        $this->db->set('ST_1', '1');

                        $this->db->where('PRI_ART_ID', $idarticolx->ART_ID);

                        $this->db->update();

                    } else {

                        $this->db->from('list_prices');

                        $this->db->set('PRI_ART_ID', $idarticolx->ART_ID);

                        $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);

                        $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));

                        $this->db->set('id_furnizor', '2');

                        $this->db->set('last_update', time());

                        $this->db->set('ST_1', '1');

                        $this->db->insert();

                    }

                }

            }

        }

    }

}   

2 个答案:

答案 0 :(得分:0)

当您假设代码中的所有内容都有效时,就会出现问题。在您的情况下,您必须在使用之前验证所有数据,以确保它存在。

第一个问题:

$this->db->select('A.*');
$this->db->select('B.adaos');
$this->db->from('tof_articles AS A');
$this->db->join('list_suppliers AS B','B.SUP_ID=A.ART_SUP_ID');
$this->db->where('A.AUG_NR',$articles[$n]['PartNo']);
$this->db->where('A.ART_SUP_ID',$articles[$n]['TDBrandId']);
$idarticolx=$this->db->get()->row();
// $this->output->enable_profiler(TRUE);
// echo "select A.*, B.adaos form tof_articles AS A join list_suppliers AS B, B.SUP_ID=A.ART_SUP_ID where A.ART_ARTICLE_NR = '{$articles[$n]['PartNo']}' AND A.ART_SUP_ID '{$articles[$n]['TDBrandId']}'";
// echo "<pre>";
// die(print_r($idarticolx));
$this->db->select('*');
$this->db->from('list_prices AS A');
$this->db->where('PRI_ART_ID',$row['id_articol']);
$b=$this->db->get();
if ($b->num_rows() > 0) {
    $this->db->from('list_prices');
    $this->db->set('PRI_PRICE_AC');
    $this->db->set('PRI_PRICE');
    $this->db->set('id_furnizor', '43');
    $this->db->set('last_update', time());
    $this->db->set('ST_1', '1');
    $this->db->where('PRI_ART_ID',$idarticolx->ART_ID);//this is line 349
    $this->db->update();
} else {
    $this->db->from('list_prices');
    $this->db->set('PRI_ART_ID' , $row['id_articol']);
    $this->db->set('PRI_PRICE_AC', $articles[$n]['PriceWoVat']);
    $this->db->set('PRI_PRICE', ceil($articles[$n]['PriceWoVat']));
    $this->db->set('id_furnizor', '43');
    $this->db->set('last_update', time());
    $this->db->set('ST_1', '1');

    $this->db->insert();
}

您假设变量$idarticolx中存在结果。 而不是:

if ($b->num_rows() > 0) {

使用

if ($b->num_rows() > 0 && $idarticolx) {

第二个问题:

if  ($articles['Description'] != ''){
    $this->db->select('A.*');
    $this->db->select('B.adaos');
    $this->db->from('tof_articles AS A');
    $this->db->join('list_suppliers AS B','B.SUP_ID=A.ART_SUP_ID');
    $this->db->where('A.ART_ARTICLE_NR',$articles['Article']);
    $this->db->where('A.ART_SUP_ID',$articles['Brand']);
    $idarticolx=$this->db->get()->row();

    $this->db->select('*');
    $this->db->from('list_prices AS A');
    $this->db->where('A.PRI_ART_ID',$idarticolx->ART_ID);
    $b=$this->db->get();

    if ($b->num_rows() > 0) {
        $this->db->from('list_prices');
        $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);
        $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));
        $this->db->set('id_furnizor', '2');
        $this->db->set('last_update', time());
        $this->db->set('ST_1', '1');
        $this->db->where('PRI_ART_ID', $idarticolx->ART_ID);
        $this->db->update();
    } else {
        $this->db->from('list_prices');
        $this->db->set('PRI_ART_ID', $idarticolx->ART_ID);
        $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);
        $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));
        $this->db->set('id_furnizor', '2');
        $this->db->set('last_update', time());
        $this->db->set('ST_1', '1');
        $this->db->insert();
    }
}

您再次假设在变量$idarticolx中有结果。代码应该是:

if  ($articles['Description'] != ''){
    $this->db->select('A.*');
    $this->db->select('B.adaos');
    $this->db->from('tof_articles AS A');
    $this->db->join('list_suppliers AS B','B.SUP_ID=A.ART_SUP_ID');
    $this->db->where('A.ART_ARTICLE_NR',$articles['Article']);
    $this->db->where('A.ART_SUP_ID',$articles['Brand']);
    $idarticolx=$this->db->get()->row();

    if ($idarticolx) {
        $this->db->select('*');
        $this->db->from('list_prices AS A');
        $this->db->where('A.PRI_ART_ID',$idarticolx->ART_ID);
        $b=$this->db->get();

        if ($b->num_rows() > 0) {
            $this->db->from('list_prices');
            $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);
            $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));
            $this->db->set('id_furnizor', '2');
            $this->db->set('last_update', time());
            $this->db->set('ST_1', '1');
            $this->db->where('PRI_ART_ID', $idarticolx->ART_ID);
            $this->db->update();
        } else {
            $this->db->from('list_prices');
            $this->db->set('PRI_ART_ID', $idarticolx->ART_ID);
            $this->db->set('PRI_PRICE_AC', $articles['PriceWithVAT']);
            $this->db->set('PRI_PRICE', ceil($articles['PriceWithVAT']*(100+$idarticolx->adaos)/100));
            $this->db->set('id_furnizor', '2');
            $this->db->set('last_update', time());
            $this->db->set('ST_1', '1');
            $this->db->insert();
        }
    }
}

另外,验证get_rest_autonet函数是否重新生成响应,json_decode解码了json字符串。

永远不要忘记任何事情,否则你的代码将成为炸弹,你将成为一个糟糕的程序员。

答案 1 :(得分:-1)

我在你的代码中看不到类似

的内容
$this->load->database();

$this->load->library('database');

你可能在autoload配置中有它,但也许它不在那里。 检查您的自动加载配置 应用/配置/ autoload.php 并检查数据库是否在$autoload['libraries']

数组中