Reputation: 1128
I have a MySQL table named tbl_br_office_sic. The table has 4 fields, officeid, servicetypeid, name,and naics_id.
I only want all the naics_id's of a certain officeid.
So in my model, I have a function that queries and returns the data to be used in the controller.
The function is defined as follow:
function getNaics($officeid){
$this->db->select('naics_id');
$this->db->from("tbl_br_office_sic");
$this->db->where('officeid', $officeid);
$data = $this->db->get();
return $data;
}
The query the function generates should be like:
select naics_id from tbl_br_office_sic where officeid = $officeid;
The $officeid variable has a valid value. However, the $data variable returned is an empty array. What did I possibly do wrong in the syntax or anywhere else?
Upvotes: 0
Views: 71
Reputation: 19
public function getNaics($officeid= false){
if($officeid=== false)
{
$this->db->order_by('name','ASC');
$query = $this->db->get('tbl_br_office_sic');
return $query->result_array();
}
$this->db->order_by('name','ASC');
$query = $this->db->get_where('tbl_br_office_sic', array('officeid' => $officeid));
return $query->row_array();
}
You can try this, thanks
Upvotes: 0
Reputation: 4033
Please use to get result result() or result_array()
function getNaics($officeid){
$this->db->select('naics_id');
$this->db->from("tbl_br_office_sic");
$this->db->where('officeid', $officeid);
$data = $this->db->get()->result_array();
return $data;
}
Upvotes: 0
Reputation: 7997
you are not getting any results, you need to use ->result()
. The correct approach for your example is:
function getNaics($officeid){
$this->db->select('naics_id');
$this->db->from("tbl_br_office_sic");
$this->db->where('officeid', $officeid);
$query= $this->db->get();
$data = ($query->num_rows())? $query->result():false;
return $data;
}
this executes the query and returns the results (or false, in case no records found)
more info on Generating Query Results
Upvotes: 4