Vajiheh Habibi
Vajiheh Habibi

Reputation: 479

where condition is not working correctly when value is false for varchar column in mysql - Yii2

In database I have some cars with status and mandatory code, data type for code is varchar(50) and not null.

In the code snippet below,

    $car = Car::find()->where(['!=', 'status', Car::STATUS_DELETED])
               ->andWhere(['=', 'code',$userCode])->one();

$userCode variable come from user interface and always is string code but rarely users input not valid value and before run this code,$userCode value convert to (bool)false, in this case final query result in the Yii debugger window is

SELECT * FROM `cars` WHERE (`status` != 2) AND (`code` = FALSE)

and the output line of the database look like the andWhere part has been removed

Note that all rows in my table have a valid value for the code field, and I expect in this case to have no record in output.

Could you explain to me why this happens?

Upvotes: 0

Views: 253

Answers (1)

Amit Verma
Amit Verma

Reputation: 2490

comparison is done at database level. In database the datatype for code is varchar(50). So, the comparison code = FALSE will not work.

If the code column has FALSE value then you need to compare as string.

SELECT * FROM `cars` WHERE (`status` != 2) AND (`code` = 'FALSE')

What do you mean by "I expect in this case to have no record in output."

Upvotes: 0

Related Questions