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");
Related
is there a way to order codeigniter select query by a specific value?
I know that it can be done in mysql from this answer, but I'm wondering if there is a "codeigniter" way of doing it, here is what I've tried:
$this->db->select('...');
$this->db->from('table_one');
$this->db->join('table_two', 'table_one.some_id = table_two.id', 'inner');
$this->db->where('city',$city);
// this gives me error
$this->db->order_by("table_two.id=$id", "desc");
$query = $this->db->get();
return $query->result_array();
This gives me the unknown column error.
yes this is possible, but you need quotes:
$this->db->order_by("table_two.id='$id'", "desc");
hint: you can always doublecheck your query like this:
echo $this->db->last_query();die;
I use this code to display data according to the user who is logged in, and display their data.
but with this syntax the data that appears does not match the defined data, and also other user data sometimes appears too.
$id=$this->session->userdata('ses_id');
$this->db->SELECT('*');
$this->db->from('doc_priv_std');
$this->db->join('acc',' doc_priv_std.id_acc=acc.id_acc');
$this->db->where('doc_priv_std.id_acc ',$id,('and
type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"'));
$query = $this->db->get();
return $query->result_array();
please help so that the data that appears as defined.
Your syntax is wrong - you can't do this with your way - you have to organise your Query properly - try the following
$id=$this->session->userdata('ses_id');
$arrExtension = ['.docx', '.pdf', '.xls', '.xlsx', '.ppt', '.pptx', '.zip', '.rar'];
$this->db
->select('*')
->from('doc_priv_std')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('acc.id_acc', $id)
->group_start();
foreach($arrExtension AS $strExtension)
{
$this->db->or_like('type_doc', $strExtension);
}
$query = $this->db->group_end()->get();
return $query->result_array();
Use var_dump($this->db->last_query()); before your return to see the db query you are actually running to help with debugging, but it could be that you where statement is wrong? Try this instead:
$where = 'WHERE type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"';
$this->db->select('*')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('doc_priv_std.id_acc', $id)
->where($where);
$query = $this->db->get();
return $query->num_rows() > 0 ? $query->result_array() : FALSE;
I am using active record on an old codeigniter installation and I am running into problems executing multiple queries using the get_where function. for instance the following code
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
the first query generates
SELECT * FROM (`activation`) WHERE `email` = 'test#yahoo.com'
the second one throws me for a loop and generates
SELECT * FROM (`activation`, `users`)
WHERE `email` = 'test#yahoo.com' AND `email` = 'test#yahoo.com'
Am i supposed to be clearing something?
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'))->row();
Use this:
$this->db->select('*')->where('email','test#yahoo.com')->get('activation')->result()
You need to retrieve the results from your query with $this->db->query->result();
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_1 = $this->db->query->result();
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_2 = $this->db->query->result();
or if you want to return an array use result_array(); instead of result();
If you use multiple get_where statements in same function then it will be concatenated.
So Better use a condition for those two conditions to execute a single query at a time.
For example,
if(//condition 1)
{
$query=$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
}
else{
$query=$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
}
//finally get the active record.
echo $this->db->last_query();
Otherwise you wont achieve.
Please mention the purpose to get two table records.
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_1 = $this->db->get()->result_array();
this will execute the first query
and then
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_2 = $this->db->get()->result();
this will execute the second query then u can do below code to print the two results
echo "<pre>";print_r($result_1);print_r($result_2);
echo "</pre>";
You need to use it like this:
$this->db->get_where('users', array('email'=>'test#yahoo.com'))->result();
Well apart from using get_where you can simply use it like this:
$this->db->select('*');
$this->db->from('activation');
$this->db->where('email','test#yahoo.com');
or
$this->db->where(array('email'=>'test#yahoo.com'));
$this->db->get_where($table, $where, $limit, $offset)
$where = array('FIELD_NAME'=>value)
result_array();
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;
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.