I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set).
Have a look at it and tell me what is wrong with it.
function sbsn($serial){
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('asset_types,asset_brands,assets');
$this->db->where('assets.type_code','asset_types.code');
$this->db->where('assets.brand_code','asset_brands.code');
$this->db->where('serial_no',$serial);
$result = $this->db->get();
return $result;
}
hi with db get you are only getting result set not record to get results from result you have to the following thing
$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array
you can use $result to perform the same above things
$result->result();
$result->row();
for more information read generating result user guide
change:
$result = $this->db->get();
to
$result = $this->db->get()->row_array(); //to get single row
//OR
$result = $this->db->get()->result_array(); //to get multiple rows
OR try using JOIN, like
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
The problem can be easily solved by joining the tables in the following way.
$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
Related
Quite a simple thing to ask and must be discussed many times, but I still not able to get the result of $this->db->last_query();.
$this->db->select('count(*) as totalverified,res_sales.upduser, employee.name');
$this->db->from('res_sales');
$this->db->join('employee','employee.user_id = res_sales.upduser');
$this->db->where('date>=', $fromdate);
$this->db->where('date<=', $todate);
$this->db->where('verificationnumber<>', '');
$this->db->where('verificationnumber<>', NULL);
$this->db->group_by('res_sales.upduser');
$this->db->group_by('employee.name');
$q = $this->db->get();
$str = $this->db->last_query();
print_r($str);
if ($q->num_rows() > 0)
{
return $q->row();
}
return FALSE;
Above is the code in my model function. I am not able to get the result as expected to want to see what the query is being run at backend.
Thanks.
Danish
I figured out the problem. I have to write a statement just above my query i.e:
$this->db->save_queries = TRUE;
After this write your query and than write $this->db->last_query(); it is showing the last query now.
Sample:
$this->db->save_queries = TRUE;
$str = $this->db->last_query();
echo $str;
Cheers.
Create a helper function and use it anywhere in your project:
function lq(){ echo $this->db->last_query(); die; }
and call to controller and model like:
lq();
I want to ask why are printing query like array simply use
$str = $this->db->last_query();
echo $str;
I'm trying to insert data from a table in MySql to other table , but I need to insert a new key before , so I tried this:
$this->db->trans_start();
$this->db->insert($this->table, $data);
$prodId = $this->db->insert_id();
$this->db->from('tmp_prod_icms');
$this->db->where('CADKEY', $this->session->userdata('key'));
$tmpICMS = $this->db->get()->result();
foreach($tmpICMS as $r) { // loop over results
$r["CADPROCOD"] = $prodId;
$this->db->insert('prod_icms', $r);
}
$this->db->trans_complete();
But when I try to insert the key "CADPROCOD" the app just stops, don't give even a error, just stops
Have you checked the logs? In the MySQL ones you should most likely find the error and why it fails.
I found the error, I use $tmpICMS = $this->db->get()->result();
And this return a object, so I can't manipulate this like a Array, so i just need to change for $tmpICMS = $this->db->get()->result_array();
So this work for me
I am trying to write this below query in codeigniter format and getting some problem with the aggregate function:
$stmt = "select sum(subscription_amt) as samt, bill_month,
sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt
from pf_bill_det
where trim(pf_number)='$pfno'
and fin_year='$fyear'
and aproved='Y' group by bill_month";
$query = $this->db->query($stmt);
This query ending up with an error loan_refund_amt*no_of_loan_installment+error_amt is not a column. Please help me how to write this query using codeigniter query format.
why don't you try this
$this->db->select("COUNT(*) AS MyCount");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum, username, password");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
BUT
In simple query function you can use
$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");
$query->result(); \\ Returns an array of objects
$query->result_array(); \\ Returns result as a pure array
$query->row(); \\ Returns a single result and first row
Try this,
$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month");
I want to perform this query wth active records :
SELECT * FROM tablename WHERE status = 'A' AND name LIKE 'test'
And i want it to return an array because i want to encode it to json later, so i need to use result_array();
So i tried something like this :
$query = $this->db->select('*')->from('tablename')->where('status', 'A');
$query->like('name', 'test')->get()->result_array();
return $query;
But i got this message when i tried to encode it to json :
type is unsupported, encoded as null
What should i do? Thanks for your help.
Try this:
$data = array();
$rs = $this->db->where('status', 'A')->like('name', 'test')->get('tablename');
if($rs->num_rows()> 0){
$data = $rs->result_array();
}
return $data;
this structure of code doesn't give any error :-
$this->db->where('status', 'A');
$this->db->like('name', 'test')
$query=$this->db->get(tablename);
$data=$query->result();
$json_data=json_encode($data);
I got the following snippet to get some userrights out of the db:
$tmp = "SELECT REPLACE(group_concat(CAST(".$role." AS CHAR)),',','') AS rights FROM functionrights ORDER BY id ASC";
$query = $this->CI->db->query($tmp);
if($query->num_rows()>0){
$row = $query->row();
return $row->rights;
This returns nothing. If I execute the statement direct everything is ok?! What is wrong here?
Change $this->CI->db->query to $this->db->query
The ->CI is not necessary. Also, you have no } tag (but that could be just a copy/paste thing).
Besides that: ->row() returns one result, while ->result() returns all results.