0

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.

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi rob006 - can you help me build a query like I am comparing ordered classes with the booked classes. so instead of generally telling for unavailabilty, I can tell more specifically which slot is not available and to remove that specific slot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.