$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.
Related
I am working on codeigniter project, I need to migrate whole database mysql to sql server, for that i am getting issue in select query, I can see in sql server round brackets are not supported around the table name, here is my codeigniter query
$this->db->_protect_identifiers=false;
$this->db->select('*')->from('tb_card',false);
$this->db->where('company_id',$this->company_id,FALSE)->get()->row_array();
This select query generates below query
SELECT * FROM (tb_card) WHERE company_id = 27
You can see there is round brackets around the table name, i want to remove this, can anyone please help me to resolve this issue ?
Finally got the solution
(/system/database/drivers/odbc/odbc_driver.php
In this path need to change the function like this way
function _from_tables($tables)
{
/*if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';*/
if ( ! is_array($tables))
{
return strstr($tables, ',') ? '('.$tables.')' : $tables;
}
else
{
return count($tables) > 1 ? '('.implode(', ', $tables).')' : end($tables);
}
}
Can you able use simple $this->db->query($sql) method, instead of Active Record.
$result = $this->db->query('SELECT * FROM tb_card WHERE company_id = 27');
printr($this->db->last_query());
printr($result->result());
you can use $this->db->get(); if you want select * (all)
for example, is something like....
function getcompany($company_id)
{
$this->db->where('company_id',$company_id);
$query = $this->db->get('tb_card');
return $query->result();
}
those function will return like so
SELECT * FROM 'tb_card' WHERE 'company_id' = $company_id
hope its answer your question.
please use this arcsecond symbol in table name tb_card
$this->db->_protect_identifiers=false; $this->db->select('*')->from('`tb_card`',false); $this->db->where('company_id',$this->company_id,FALSE)->get()->row_array();
I am trying to make this query with laravel query builder but i did not understand how i can do this:
Query
select * from `data_table_1486794412` where (`column_2` = '2014' or `column_2` = '2015') and (`column_1` = 'GNDU')
My Code is:
$dbObj = DB::table($datatableName->dataset_table);
$dbObj->orWhere('column_2','2015');
$dbObj->orWhere('column_2','2015');
$dbObj->where(array('column_1','GNDU'));
Laravel Generating Query:
select * from `data_table_1486794412` where `column_2` = ? or `column_2` = ? and (`column_1` = ?)
My Table
In order to group WHERE clauses so that they're surrounded by brackets you need to pass a callback to where() call.
The following code should do the trick:
$dbObj = DB::table($datatableName->dataset_table);
$column1 = 2015;
$column2 = 2016;
$dbObj->where(function($query) use ($column1, $column2) {
$query->orWhere('column_1',$column1);
$query->orWhere('column_2',$column2);
});
$dbObj->where(array('column_1','GNDU'));
i think that's work
$dbObj = DB::table($datatableName->dataset_table);
$dbObj->orWhere('column_2','2015')->orWhere('column_2','2015')->where(array('column_1','GNDU'))
i am using orWhere in loop. can you please tell how i can use it in loop..because 2014 and 2015 are coming in form of array.
You can use whereIn rather than a loop, where you can pass in an array:
$dbObj = DB::table($datatableName->dataset_table);
$dbObj->where('column_1', 'GNDU')->whereIn('column_2', $yourArray)->get();
I am trying to get the following query:
SELECT * FROM `book_category_tree` WHERE `id` IN (8, 9, 10, 15)
According the following yii2 documentation:
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findAll%28%29-detail
If i use:
$rows = BookCategoryTree::findAll([8,9,10,15]);
It works perfect but when ever I try to use a variable with the values yii2 doesn't recognize the values:
$myValues = '8,9,10,15';
$rows = BookCategoryTree::findAll([$myValues]);
and generates the following query:
SELECT * FROM `book_category_tree` WHERE `id`='8,9,10,15'
I am getting crazy to avoid this I also tried:
$rows = BookCategoryTree::find()->where(['in','id',[$myValues]])->all();
But I have no luck.
Any ideas welcome,
Thanks
you should use where condition with operator notation like this
$rows = BookCategoryTree::find()->where( ['in', 'id', [8,9,10,15]])->all();
or
$myValues = [8,9,10,15];
$rows = BookCategoryTree::find()->where(['in','id',$myValues])->all();
if you have value in string you can use explode for build an array
$myValues =explode(',', $string_values);
I have a MySQL query which is this:
SELECT * FROM tbl_post WHERE tbl_post.post_id NOT IN
(SELECT tbl_readsave.post_id FROM tbl_readsave)
I want to convert it into Codeigniter Active records, so I used the following code segment:
$this->db->select('tbl_readsave.post_id');
$queryReadSave = $this->db->get('readsave');
$this->db->where_not_in('post_id', $queryReadSave->result_array());
$queryNewPost = $this->db->get('readsave');
if($queryNewPost->num_rows()>0)
{
return $queryNewPost->result_array();
}
else
return false;
However, the code throws me an error, which is like the following:-
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM (`tbl_readsave`) WHERE `post_id` NOT IN (Array)
Filename: /var/www/html/teamF/tharjumal/models/webservice_model.php
Line Number: 28
How can I convert the above stated query into Codeigniter Active Records format?
$queryReadSave->result_array() returns an array of arrays. You can't use that in where_not_in.
You need to loop over that and create a flat array of the IDs you (don't) want.
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = $queryReadSave->result_array();
$this->db->where_not_in('post_id', array_column($postIDs, 'post_id'));
$queryNewPost = $this->db->get('post');
array_column() only exists in PHP 5.5+. If you are on a lower version, you'll need to do something like this:
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = array_map(function($a){
return $a['post_id'];
}, $queryReadSave->result_array());
$this->db->where_not_in('post_id', $postIDs);
$queryNewPost = $this->db->get('post');
P.S. Your second table is called post, right? You'll need to update the query to use the right table.
Update: Your original query uses a subquery which is not natively supported by CodeIgniter. If you want to try this all as one query, you can use the Subquery library I created (https://github.com/NTICompass/CodeIgniter-Subqueries).
$this->db->select('*');
$this->db->from('post');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('post_id')->from('readsave');
$this->subquery->end_subquery('post_id', FALSE);
$queryNewPost = $this->db->get();
try below code:
$read_post_id = [];
$queryReadSave = $this->db->select('post_id')->get('tbl_readsave')->result_array();
if(count($queryReadSave) > 0){
foreach($queryReadSave as $row){
$read_post_id[] = $row['post_id']; // add each post id to the array
}
}
$this->db->select('*');
if(!empty($read_post_id)) $this->db->where_not_in('post_id',$read_post_id);
$post = $this->db->get('tbl_post');
print_r($post->result_array());
exit;
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);