I want to do a sql query like
select * where x = 10 and (y=12 or h=15)
How can I achieve that in CI ActiveRecord format?
See Active record reference
$where="x = 10 and (y=12 or h=15)";
$this->db->select('*');
$this->db->from('mytable');
$this->db->where($where);
$query = $this->db->get();
Try like
$sql = "SELECT * FROM TABLE_NAME WHERE x = 10 AND (y = 12 OR h = 15)";
$result = $this->db->query($sql);
Or like
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('x',10);
$this->db->AND('y',12);
$this->db->or_where('h',15);
$query = $this->db->get();
Related
I have the following query I will like to implement in CodeIgniter:
SELECT COUNT(*) FROM tickets WHERE status = "open";
The result returned will be '1' and I will like to echo the result. I have the current code query:
$this->db->count_all_results();
$this->db->select('*');
$this->db->where('status', 'Open');
$this->data['opentickets'] = $this->support_m->get();
I am trying to display the count result within the view. Any advice on how I can do this?
Please try below code to get the count the number of row with open status.
$this->db->where("status", 'Open');
$query = $this->db->get("tickets");
$this->data['opentickets'] = $query->num_rows();
Or You can use this one
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->row_array()['COUNT(*)'];
You can run query in codeigniter :
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->result_array();
How can I convert this mysqli_fetch_array in php to codeigniter?
$kueri7 = "SELECT ceil(count(kelas_id)/(select count(user_id) from tuser where matpel_id = $idmatpel)) as jumlah from tkelas";
$cocok8 = mysqli_query($con, $kueri7);
$cocok_kelas_maksimal = mysqli_fetch_array($cocok8);
Thanks in advance.
With codeIgniter, your code would look like this:
$query = $this->db->query("SELECT ceil(count(kelas_id)/(select count(user_id) from tuser where matpel_id = $idmatpel)) as jumlah from tkelas", FALSE);
$cocok_kelas_maksimal = $query->result_array();
here is my code of model
$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
getting error of unknown column complain_form.InstituteId. each and every column is same as in db
Hope this helps you :
$this->db->select(*);
$this->db->from('complain_form cf');
$this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
$this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You need JOIN:
SELECT *
FROM complain_form
JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId
JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId
using Query Builder class Codeigniter:
$row = $this->db->select(*)
->from('complain_form')
->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
->get();
return $row->result_array();
I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();
How to use the codeigniter with $this->db->query() method?
If i use active record class i would do like this:
$query = $this->db->get('tb_cash_transaction',$num,$offset);
$this->db->order_by("CURRENCY_ID", "asc");
Now i am using the $this->db->query()
$query = "SELECT * FROM tb_cash_transaction, tb_currency, tb_user where tb_cash_transaction.CURRENCY_ID=tb_currency.CURRENCY_ID and tb_cash_transaction.USER_ID=tb_user.USER_ID and TYPE='cash_out'";
$query = $this->db->query($query);
How to implement it? Thank you.
Try with this one..
$query = "SELECT * FROM tb_cash_transaction, tb_currency, tb_user where tb_cash_transaction.CURRENCY_ID=tb_currency.CURRENCY_ID and tb_cash_transaction.USER_ID=tb_user.USER_ID and TYPE='cash_out' order by CURRENCY_ID asc LIMIT $offset , $num";
$query = $this->db->query($query);
Hope this will Help.
Thanks!
Hussain