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;
Related
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;
Hi I'm new to CodeIgniter and I just want to know How will I query from my MySql Db, a Select Statement with a where clause, I know it can be searched from the net but whenever I try something I get errors, It's really frustrating. The string in the Where clause will be coming from a User Input. Thanks guys!
You can do as Mehedi-PSTU stated, however it seems as though you're a little new to this, so here's some extra information:
I'll copy Mehedi-PSTU for the most part here.
$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');
This will store the query object in the variable $query.
if you wanted to convert that to a usable array, you just perform to following.
$results = $query->result_array();
Or you can loop through it like this:
foreach($query->result_array() as $result){
// Perform some task here.
}
A better or even full understanding can probably come from:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Try something like this
$this->db->where('db_attr', $var);
return $this->db->get('table');
Try this one.
$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();
In Codeigniter with Method Chaining Style :-
$data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();
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");
Can someone tell me, if this is possible with active record - and how??
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.fi_id', 'left');
$this->db->having('table1.second_id','table2.fi_second_id', false);
$query = $this->db->get();
The problem ist, that 'table2.fi_second_id' is always treated as a string - not as a database field. Tried this with 'where' also - it's the same problem.
Thx
I think that you want the following:
$this->db->having('table1.second_id = table2.fi_second_id',false);
You may or may not apply the false parameter if you don't need escaped SQL queries.
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.