Answer the question
In order to leave comments, you need to log in
How to write a query if the variable is empty?
How to correctly write a query using Yii::app()->db->createCommand() if the variable in the condition may be empty?
$sql = $db->createCommand()
->select('`id`, `sub_id`, `min_price`,
->from('table')
->where("sub_id=$sub_id")
->query();
Answer the question
In order to leave comments, you need to log in
$sql = $db->createCommand()
->select(['id', 'sub_id', 'min_price']),
->from('table');
if(!empty($sub_id)){
$sql->where(['sub_id' => $sub_id]);
}
$sql->query();
If the where method can accept an empty string, then so be it.
The void can be different.
If you have it empty, it's '', then you need to compare like this:
$sub_id !== ''
If null, then like this
!is_null($sub_id)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question