yii2 findAll not working passing in parameter from variable - php

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);

Related

How to search for 2 values in the same field with codeigniter?

I have a database field containing keyword ids ["1","2","3","4","5","6"] and I want to search for several of these ids in the same field.
Here is the idea of what I would like to achieve:
$tags[] = "\"1\"";
$tags[] = "\"2\"";
$query = $this->db->like(array('ids_keywords' => $tags[0], 'ids_keywords' => $tags[1]));
The problem here is that codeigniter only executes the last request instead of both because the search field is same.
What would be the best solution to do this?
try the following
$arrTags = [1,2];
$this->db->group_start()
foreach($arrTags AS $strTag)
{
$this->db->like(ids_keywords, '"'.$strTag.'"');
}
$this->db->group_end();
You can write your own clauses:
$where = "ids_keywords='1' AND ids_keywords='2' OR ids_keywords='3'";
$this->db->where($where);
For safe query use question mark(?)
$sql = "SELECT * FROM some_table WHERE ids_keywords = ? AND ids_keywords = ? AND ids_keywords = ?";
$this->db->query($sql, array(1, 2, 3));
In the above example, the question mark(?) will be replaced by the array in the second parameter of query() function. The main advantage of building query this way is that the values are automatically escaped which produce safe queries. CodeIgniter engine does it for you automatically.

Dynamically creating OR conditions by passing an array to a query in MySQL PHP

I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array('Eng, 'Deu', 'Bio', 'Chemi') I want to have a SQL query that uses the values of the array in its AND clause with OR conditions like:
SELECT *
FROM classe
/* The OR conditions should be created in AND clause using array */
WHERE class = 'EFG' AND (course = 'Eng' OR course = 'Deu' OR course = 'Bio')
I trying to do it in PHP MySQL.
Any help would be really appreciated.
Thanks in Advance.
Instead of so many OR clauses, you can simply use IN(..):
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode() function to convert the array into a comma separated string, and use it in the query string generation.
The IN clause will be easier to use than ORs. If you are using PDO you can take advantage of its execute binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)

In ci Where_in is not working properly

$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.

Getting only the data from a select statement in PHP

I am getting some data from a database, and it returns an array $rows as shown below:
However the returned rows doesn't contain a pure array with the CV data. I would like just to have a pure array which has only the values.
My code:
$sql = 'SELECT CATEGORY_VALUE from '.self::TABLE_NAME. ' where E_ID = :E_ID and C_TYPE = :C_TYPE';
$where = array('E_ID'=>$id, 'C_TYPE'=> $c);
$stmt = $this->_db->query($sql,$where);
$rows = $stmt->fetchAll();
return $rows;
I think you have misunderstanding of how php arrays work. You can learn more here. http://php.net/manual/en/language.types.array.php
In PHP, you would get the value you want by doing the following:
$rows[0]['CATEGORY_VALUE'] will return 'H3
You need to use PDO::FETCH_COLUMN.
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN);

Laravel: unable to build multiple orWhere query with laravel query builder

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();

Categories