YII 1.1 - How to fetch multiple record in FETCH_KEY_PAIR format

24
January 13, 2019, at 07:40 AM

We are facing some problem for fetch record in PDO format. See below my example code:

$getCategories= Yii::app()->db->createCommand()
        ->select('id,category_name')
        ->from('content_categories')
        ->where('id IN (:id)', array(':id'=>$getContentList['content_category_value']))
        //->where("id IN ({$getContentList['content_category_value']})")
        ->queryAll();
print_r($getCategories); exit;

Output:

Array
(
    [0] => Array
        (
            [id] => 1525
            [category_name] => TV 
        )
)

Above query $getContentList['content_category_value']) returns 1525,45 but we are getting only single record.

When we manually execute query:

SELECT * FROM `content_categories` WHERE `id` IN (1525,45);

It returns:

Actually we have two category but above query fetch only one. Could you please check and help me to how we show multiple record fetch by using PDO concept.

Answer 1

Not tested but you can make a try like this,

 $getCategories= Yii::app()->db-
 >createCommand()
    ->select('id,category_name')
    ->from('content_categories')
    ->where(array('in', 'id', 
    $getContentList['content_category_value']))    
    ->queryAll();
print_r($getCategories); exit;
Answer 2

If $getContentList['content_category_value'] is a string, then it will be used in query as a string. So you're probably getting something like this:

SELECT * FROM `content_categories` WHERE `id` IN ('1525,45');

If you want to build IN condition, you need to use array of values with IN operator:

->where(['in', 'id', explode(',' $getContentList['content_category_value'])])
READ ALSO
SQL Statement loads very slow with Order By

SQL Statement loads very slow with Order By

I have this sql statement below and returns in 0149seconds:

20
Python mysql connector query same result every time

Python mysql connector query same result every time

I want to access a mysql database via mysqlconnector in python 2

44
Is there a way to fetch string value from MySQL database through Karate

Is there a way to fetch string value from MySQL database through Karate

I am having an issue while karate fetches a string value from MySQL dbreadValue works fine if it is string value it fetches from db but getting error like Unknown column 'SMEMSD22' in 'where clause' How to deal with alphanumeric data with karate

36