Codeigniter中的活动记录错误

时间:2014-03-18 16:57:03

标签: php codeigniter activerecord

我有一个使用活动记录的选择查询,我使用的是codeigniter 2.1,我的代码中出现以下错误

          `A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: database/DB_active_rec.php

Line Number: 82

`

这是我的控制器代码

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Carbonrates extends CI_Controller {

public function __construct()
{
    parent::__construct();
    //Load Dependencies     

}

// List all your items
public function index()
{
    $this->load->model('ratesmodel');

    $electricity_provider = $this->input->post('providerNo');

    $carbon_rate = $this->ratesmodel->carbon($electricity_provider);

    var_dump($carbon_rate);

    return false;
}

}

我的模特

    `    function carbon($provider) {           

        $this->db->select($provider);
        $where = array('username' => $this->session->userdata['logged_in']['username']);

        if($provider == 1 ) {

            $this->db->select('Winter_Night_Time1'); 
            $this->db->from('carbon_rates');   
            $this->db->where('username', $where);
            return $this->db->get()->result();  


        }`

我已经搜索了可能的答案,但还没有找到答案。

1 个答案:

答案 0 :(得分:0)

你的$ where是一个数组,试试:

function carbon($provider) {           

        $this->db->select($provider);
        $where = array('username' => $this->session->userdata['logged_in']['username']);

        if($provider == 1 ) {

            $this->db->select('Winter_Night_Time1'); 
            $this->db->from('carbon_rates');   
            $this->db->where($where);
            return $this->db->get()->result();  


        }

即。使用&#39;用户名&#39;来自where来电的参数...

编辑 *

DB_Active_rec.php上的第82行是选择。因此请检查您对$ provider的第一个select调用,并确保这是逗号分隔的字符串或表名数组......其他任何内容都不起作用。即if ($provider == 1)您尝试拨打$this->db->select(1);

时会收到此错误消息

TRY:

function carbon($provider) {           

    if($provider == 1 ) {

        $this->db->select('Winter_Night_Time1'); 
        $this->db->from('carbon_rates');   
        $this->db->where('username', $where);
        return $this->db->get()->result();  


    }

    $this->db->select($provider);
    $where = array('username' => $this->session->userdata['logged_in']['username']);