Joshi
Joshi

Reputation: 2790

Yii2 issue to get the result for db query

I am using this command to get if the record already exist in the database;

$query = Yii::$app->db->createCommand("SELECT IF(EXISTS(SELECT * FROM `order_item` 
WHERE `date` = '$date'  AND `start_time` = '$starttime'), 1, 0)");

$result=$query->queryAll();

var_dump($result);exit;

Now the result I am getting for dump is like:

array(1) { [0]=> array(1) { ["IF(EXISTS(SELECT * FROM `order_item` 
WHERE `date` = '2018-12-03' AND `start_time` = '10:15:00'), 1, 0)"]=> string(1) "0" } }

whereas I want the result as just 1 or 0 like

if ($result==1){
//do something;
}

as if I am running the same query in phpmyadmin - I am getting the result as 0 or 1

How I can achieve the same from the Query in Yii2.

Upvotes: 0

Views: 92

Answers (1)

rob006
rob006

Reputation: 22174

queryAll() returns all fields from all rows as arrays. If you want to get single value from first field of first row, you need to use queryScalar().

$result = $query->queryScalar();

Upvotes: 2

Related Questions