Your code may be vulnerable for SQL Injection. You should use prepared statements for passing values to query:
$questions = Yii::$app->db
->createCommand("SELECT * FROM `def_questions` where `question` LIKE :question", [
':question' => "%$question%",
])
->queryAll();
Note that you should also escape some special characters from searched value, to make it work correctly with LIKE
operator (for example treat %
as %
instead of "anything", see How to use a percent (%) in a LIKE without it being treated as a wildcard?):
$question = strtr($question, [
'%' => '\%',
'_' => '\_',
'\\' => '\\\\',
]);
$questions = Yii::$app->db
->createCommand("SELECT * FROM `def_questions` where `question` LIKE :question", [
':question' => "%$question%",
])
->queryAll();
The easiest way to do the whole thing is probably by using Query
:
$questions = (new \yii\db\Query())
->from('def_questions')
->where(['like', 'question', $question])
->all();
Query
will do escaping for you and will return results for SQL query:
SELECT * FROM `def_questions` where `question` LIKE '%What is your Name%'