I am looking for query in Codeigniter works like where OR, AND both works.
This is working Perfect!
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
query is:
Need to add OR in where with $srcArr1['ass_id']=12;
I try but not working.
$srcArr1['ass_id']=12;
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$this->db->where_in($srcArr1);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
Thank You, in Advance for help
You can use or_where_in to achieve the same,
$this->db->or_where_in($srcArr1);
Official doc.
$this->db->where($srcArr2);
$this->db->or_where($srcArr1);
https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-dataenter link description here
Related
I just started learning Codeigniter 4. My query always generates NULL and I don't know why. How can I see the generated SQL Select command just like Codeigniter 3?
In Codeigniter 3 this command does the job:
echo $this->db->last_query();
And this is my controller code in Codeigniter 4 that I need to get the generated query:
$cityModel = new CityModel();
$cities = $cityModel
->select('city.name AS cityName')
->select('county.name AS countryName')
->select('province.name AS provinceName')
->join('province', 'city.province_id = province.id', 'left')
->join('county', 'city.county_id = county.id', 'left')
->result();
Update:
I tried this code but it's returning an empty string:
var_export((string)$cityModel->db->getLastQuery());
This should display the final query:
$cityModel->getLastQuery()->getQuery()
In CI 4 Refer Doc
you can use getLastQuery() as
$query = $db->getLastQuery();
echo (string)$query;
This code will help you to get the last query in codeigniter 4.
$this->db = \Config\Database::connect();
$data = $builder->get()->getResult();
echo $this->db->getLastQuery(); die;
You can use getCompiledSelect it will return the query SELECT command.
$sql = $cityModel->getCompiledSelect();
echo $sql;
Following Code will work in CI 4
$this->db->getLastQuery()->getQuery();
Simply just use $this->db->getLastQuery();
This worked for me
$myModel = new MyModel();// your any model
$myModel->db->getLastQuery()->getQuery();
$this->db is not directly accessible from controller, but if you have any Model instance then you can have the full access to db object.
Add this code in your class
$db = \Config\Database::connect();
$query = $db->getLastQuery();
echo $query;
$menus = implode(',', $menu_id);
$this->db->where_in('rgroup_id',$menus);
$query = $this->db->get('rights_group');
please give me suggestion
Please check this code.
$menu_id = "1,2,3";
$menus = explode(',', $menu_id);
$this->db->where_in('rgroup_id', $menus);
$query = $this->db->get('rights_group');
You should use array as value try this
$menus = [1, 3, 4];
$this->db->where_in('rgroup_id', $menus);
$query = $this->db->get('rights_group');
more : https://codeigniter.com/user_guide/database/query_builder.html#CI_DB_query_builder::where_in
May be bug on Codeigniter,
I copy query and run on mysql and it has no error, but codeigniter still error near WHERE ... IN ()
So I run by native and it work like a charm.
$this->db->save_queries = TRUE;
$this->db->from('rights_group');
$query = $this->db->last_query();
return $this->db->query($query)->result();
Don't forget:
$this->db->save_queries = TRUE;
Change
$this->db->where_in('rgroup_id',$menus);
to
$this->db->where_in('rgroup_id',$menus,false);
Look at the generated query below.
SELECT * FROM `my_table` WHERE `my_id` IN(‘1,2,3’)
There is a quote before 1 and after 3. The query should be
SELECT * FROM `my_table` WHERE `my_id` IN(1,2,3)
to work it as expected.
We do not need to implode the array: directly pass array in where_in. You will get result. I have tried and it worked.
For example, I have this MongoDB:
In yii2, I want to write a query to find an email = 'abc'. I have this code:
$query->select([])->from('Post')->where(['Comments.Email' => 'abc']);
But it does not work; please help me, as I'm beginner in Yii2 and MongoDB.
You can try this
Document::find()->where(["comments"=>["document"=>["email"=>"abc"]]])->all();
$res = $collection->find(["comments.email"=>"abc"]);
$rows = $res->toArray();
From documentation:
https://www.yiiframework.com/extension/yiisoft/yii2-mongodb/doc/api/2.1/yii-mongodb-collection#find()-detail
You can do that but using this query below,
$order = (new Query)->select([])->from('post')->where(['email' => 'abc'])->all();
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 have a mysqli-resultset, which will only contain 1 row. I get the value like this at the moment:
$result = mysqli_query($link,$query)->fetch_array(MYSQLI_NUM);
$result = $result[0];
Isn't there a more straight-forward way to do this?
In PHP 5.4:
$result = mysqli_query($link,$query)->fetch_array(MYSQLI_NUM)[0];
but it's missing error handling.
Instead of using fetch_array, why not just use fetch_row if you only ever want a single row?
$result = mysqli_query($link,$query)->fetch_row()