首页 > 解决方案 > 我正在尝试使用 fetch_assoc() 但它给出了错误未定义的方法

问题描述

致命错误:未捕获错误:调用 /var/www/html/magento22/mage.php:145 中未定义的方法 Magento\Framework\DB\Statement\Pdo\Mysql::fetch_assoc()

这是我的代码

$data = array();
/* ORDERS */

$result = $write->query('select c.sku, v.value, if(iv.qty = 0 and 
                            (if(iv.use_config_backorders = 1,\'Allow Backorder\',if(iv.backorders = 101,\'Allow pre-order\',\'No Backorder\'))) = \'No Backorder\'
                            , \'disabled\',\'enabled\') as status, iv.qty, 
                            if(iv.use_config_backorders = 1,\'Allow Backorder\',if(iv.backorders = 101,\'Allow pre-order\',\'No Backorder\')) as stock_status,
                            if(e.product_id is null,\'No\',\'Yes\') in_wow,
                            d.value as pre_date
                            from catalog_product_entity c
                            join catalog_product_entity_varchar v on v.entity_id = c.entity_id and v.attribute_id = 71
                            join catalog_product_entity_int s on s.entity_id = c.entity_id and s.attribute_id = 96
                            join cataloginventory_stock_item iv on iv.product_id = c.entity_id
                            join catalog_product_entity_varchar d on d.entity_id = iv.product_id and d.attribute_id = 182
                            left join catalog_category_product e on e.product_id = c.entity_id and category_id = 61
                            join catalog_product_website w on w.product_id = c.entity_id
                            where c.type_id = \'simple\' and w.website_id = 1 and s.value = 1
                        ');

if(is_object($result))
{   
    while ($resultsArray = $result->fetch_assoc())
    {
        if(empty($data))
           $data[] = array_keys($resultsArray);

        $data[] = $resultsArray;
    } var_dump($data);
}

没有连接问题,而且我的对象 $result 也有值。所以没问题。

标签: phppdomagento2

解决方案


尝试改变:while ($resultsArray = $result->fetch_assoc())

到:while ($resultsArray = $result->fetchAll(\PDO::FETCH_ASSOC))

或者可以通过while ($resultsArray = $result->fetch(\PDO::FETCH_ASSOC))


推荐阅读