phpyii2bindvalue

Yii2 bindValue in LIKE condition


How can I do a wildcard search using bindValue(:name, $name) in LIKE condition given this query:

$post = Yii::$app->db->createCommand('SELECT * FROM fruits WHERE name LIKE %:name%')
       ->bindValue(':name', 'apple')
       ->queryOne();

Solution

  • I did this and it does as I need.

    $post = Yii::$app->db->createCommand('SELECT * FROM fruits WHERE name LIKE :name')
       ->bindValue(':name', '%apple%')
       ->queryOne();