php - 模型中的查询在更改后停止工作

标签 php mysql arrays codeigniter

我有这个有效的 CI 模型函数 get_info:

function get_info($item_id)
{  

    $this->db->from('items');
    $this->db->where('item_id', $item_id);


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

    if($query->num_rows()==1)
    {
        return $query->row();
    }
    else
    {

        $item_obj=new stdClass();
        $fields = $this->db->list_fields('items_tier_prices');

        foreach ($fields as $field)
        {
            $item_obj->$field='';
        }

        return $item_obj;
    }
}

我需要使用此自定义查询修改上述函数 get_info:

function get_info($item_id)
{
$sql_query = $this->db->query("select * from (select items.name as name, items.category as category, items.supplier_id as supplier_id, items.item_number as item_number, items.product_id as product_id, items.description as description, items.size as size, items.tax_included as tax_included, items.cost_price as cost_price, items_tier_prices.unit_price  as unit_price, items.promo_price as promo_price, items.start_date as start_date, items.end_date as end_date, items.reorder_level as reorder_level, items.item_id as item_id, items.allow_alt_description as allow_alt_description, items.is_serialized as is_serialized, items.image_id as image_id, items.override_default_tax as override_default_tax, items.is_service as is_service, items.deleted as deleted

    from items
LEFT JOIN items_tier_prices
ON items.item_id=items_tier_prices.item_id
LEFT JOIN price_tiers
ON price_tiers.id=items_tier_prices.tier_id
where
 price_tiers.name='jendela kaca mati single'
 and items.deleted=0

 union
select items.*
from items
left join items_tier_prices
ON items.item_id=items_tier_prices.item_id
where items_tier_prices.item_id is null
and items.deleted=0
) x
where x.item_id=".$item_id);


$query = $this->db->query($sql_query);

if($query->num_rows()==1)
{
    return $query->row();
}
else
{
    $item_obj=new stdClass();

    $fields = $this->db->list_fields('items');
    $fields = $this->db->list_fields('price_tiers');
    $fields = $this->db->list_fields('items_tier_prices'); 

  foreach ($fields as $field)
    {
        $item_obj->$field='';
    }

    return $item_obj;
}

}

问题是在修改查询和 list_field 之后,函数 get_info 不再起作用。

我确定查询在 phpmyadmin 中有效,并且函数中产生的查询结果是相同的。它产生相同的列名和仅 1 行结果。

但我不确定:

    $fields = $this->db->list_fields('items');
    $fields = $this->db->list_fields('price_tiers');
    $fields = $this->db->list_fields('items_tier_prices'); 

我为停止函数 get_info 工作所做的修改有什么问题?

最佳答案

你正在调用 $this->db->query 两次。

将您编写查询的代码更改为:

$sql_query = "select * from (select items.name as name
             ..etc..";

然后按照您的脚本执行查询:

$query = $this->db->query($sql_query);

如果您的查询是正确的,您将返回一行。请记住处理查询最终返回 0 行或多于 1 行的异常。

您还可以检查您的浏览器控制台。如果您的查询语法有错误,您会发现 500 Internal Server Error,它允许您检查可能导致错误的原因。

关于php - 模型中的查询在更改后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35702845/

相关文章:

mysql - SQL 中缺少 ID 号而停止自动递增?

java.sql.SQLSyntaxErrorException :

javascript - JQuery AJAX 调用 php 函数

javascript - jQuery 读取非空和可见的文本字段

php - 我们可以在 php 类的一个方法中使用两个 mysql 查询吗?

javascript - 通过 Javascript 计算数组中排列的计数而不重复

c++ - 如何检查一个数字被输入了多少次

python - 使用numpy和cv2处理大型二进制图像数组

php - 在 Yii 中查询关系模型

php - 如果 SQL IN 语句中的 Select 返回 NULL,则选择 ALL