简体   繁体   中英

Cannot get query result from Model to Controller in Codeigniter

Trying to get result from database by calling stored procedure in code-igniter. Below is my controller & model. Model is working fine getting result from stored procedure and can show the result when I do print_r() in model. But I am not able to get the result of query in controller method. Not able to understand the error here.

Controller:

function weeklysalesstatus()
{
    $this->data['rpt']  =$this->reports_model->weeklysalesstatus(true);
    $this->data['page_title'] ='Weekly Sales Status [ Residential ]';
    print_r("here...");
    $this->page_construct('reports/weeklysalesstatus', $this->data);
}

Model:

public function weeklysalesstatus($sender = false)
{
    $sql = 'select * from region';
    $sql1 = 'CALL GetWeeklySalesStatus()';
    $query = $this->db->query($sql1); 

    if(count($query) > 0) {
        foreach (($query->result()) as $row) {
            $data[] = $row;
            //var_dump($data);
        }
        return $data;
    } else { 
        print_r("no rows");
    }
    return $data;
}

The issue is somewhere in this line : $this->data[$rpt] = $this->reports_model->weeklysalesstatus(); But can't figure it out. Thanks

EDIT:

These errors arise only when I call Stored Procedure CALL. For other queries it work perfectly. So is there any settings I need to do on Codeigniter DB class for calling a Stored procedure thru active record?

$q = $this->db->query('CALL GetWeeklySalesStatus');

Change this line to

$q = $this->db->query('CALL GetWeeklySalesStatus')->result();

In Model

public function weeklysalesstatus()
{
    //$sql = 'select * from region';
    $sql1 = 'CALL GetWeeklySalesStatus()';
    $query = $this->db->query($sql1); 

    if(!empty($query)) 
    {
        $data = $query->result_array();
        return $data;
    }
    else 
    { 
        return FALSE;
    }
}

In Controller

function weeklysalesstatus()
{
    $rpt  =$this->reports_model->weeklysalesstatus();

    if ($rpt == FALSE) {
        echo "Empty";
    } else {
        $data['rpt'] =  $rpt;
    }

    $data['page_title'] ='Weekly Sales Status [ Residential ]';
    $this->page_construct('reports/weeklysalesstatus', $data);
}

Modify the controller like the following hope it works

function weeklysalesstatus()
        {
             $this->data['rpt']   =$this->reports_model->weeklysalesstatus();
             $this->data['page_title'] ='Weekly Sales Status';
             $this->page_construct('reports/weeklysalesstatus', $this->data);
        }

your model

public function weeklysalesstatus($sender = false)
{
    $sql = 'select * from region';
    $sql1 = 'CALL GetWeeklySalesStatus()';
    $query = $this->db->query($sql1); 
    $data["result"] = array();
    if(count($query) > 0) {
        foreach (($query->result()) as $row) {
           $data["result"][] = $row;
           //var_dump($data);
        }
    }
    return $data;
}

your controller

function weeklysalesstatus()
{
   $data['rpt']  =$this->reports_model->weeklysalesstatus(true);
   $data['page_title'] ='Weekly Sales Status [ Residential ]';
   print_r("here...");
   $this->page_construct('reports/weeklysalesstatus', $data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM