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

时间:2022-06-23 14:58:37

I have a car parts website with tecdoc database and I included a provider for the prices and stock,via their api.

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

I have an error when entering a product page

输入产品页面时出错

A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/tree.php Line Number: 349

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

THis is the line on 349:

这是349的一行:

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

This is the code:

这是代码:

        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 个解决方案

#1


The problems arise when you assume everything worked in your code. In your case, you must validate all the data before using it, to make sure it exists.

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

The first problem:

第一个问题:

$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();
}

You are assuming that in the variable $idarticolx there is a result. Instead of:

您假设在变量$ idarticolx中有结果。代替:

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

use

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

The second problem:

第二个问题:

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();
    }
}

You are assuming again that in the variable $idarticolx there is a result. The code should be:

您再次假设在变量$ 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();
        }
    }
}

Also, validate that get_rest_autonet function resturns a response and that json_decode decoded a json string.

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

Never asume anything or your code will be a bomb and you will be a bad programmer.

从来没有任何东西或你的代码将是一个炸弹,你将是一个糟糕的程序员。

#2


I dont see in your code something like

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

$this->load->database();

or

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

you might have it in the autoload config but maybe its not there. check your autoload config at application/config/autoload.php and check if the database is in the array of $autoload['libraries']

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

#1


The problems arise when you assume everything worked in your code. In your case, you must validate all the data before using it, to make sure it exists.

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

The first problem:

第一个问题:

$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();
}

You are assuming that in the variable $idarticolx there is a result. Instead of:

您假设在变量$ idarticolx中有结果。代替:

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

use

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

The second problem:

第二个问题:

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();
    }
}

You are assuming again that in the variable $idarticolx there is a result. The code should be:

您再次假设在变量$ 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();
        }
    }
}

Also, validate that get_rest_autonet function resturns a response and that json_decode decoded a json string.

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

Never asume anything or your code will be a bomb and you will be a bad programmer.

从来没有任何东西或你的代码将是一个炸弹,你将是一个糟糕的程序员。

#2


I dont see in your code something like

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

$this->load->database();

or

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

you might have it in the autoload config but maybe its not there. check your autoload config at application/config/autoload.php and check if the database is in the array of $autoload['libraries']

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