Smudger
Smudger

Reputation: 10809

Codeigniter - pass single query result to controller with json - if exists

Wanting to pass a single codeigniter query result to my controller using json.

my model function is:

 function get_linked_loads_qty($q){

$sql = $this->db->query("
IF EXISTS(
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
)
begin

select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'

END
ELSE BEGIN

select  1 as qtylinkedorders
from customer_Order_lines
where CustomerOrderID='$q'
group by CustomerOrderID

END
         ");

    $query = $this->db->get();
    if($query->num_rows > 0){
        foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row[qtylinkedorders])); //build an array
       }
       return $row_set;
    }
  }

and my controller is:

function get_linked_loads_qty(){

    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $data = $this->Sales_model->get_linked_loads_qty($q);
        $this->output->set_content_type('application/json')->set_output(json_encode($data));
    }

}

The sql works 100% in mssql. the codeigniter error is: <p>Error Number: 42000</p><p>[Microsoft][SQL Server Native Client 11.0][SQL Server]Must specify table to select from.</p><p>SELECT *</p>

as mentioned the query executes perfectly in mssql.

any assistance welcome. Thanks.

Upvotes: 0

Views: 517

Answers (1)

user9645
user9645

Reputation: 6806

I believe the $this->db->get() is not needed, and is what causes the error. The $this->db->query("blah...") will run the query and return the result. See CodeIgniter DB Query Docs

Upvotes: 1

Related Questions