Jan Beeck Jan Beeck - 2 years ago 374
PHP Question

How to call the SQL LIKE function in Yii2?

Receiving NULL as value when I called this SQL statement (the record exists in the MySQL DB). How can use the LIKE function in Yii2? Thanks.

$command = $connection->createCommand("SELECT user_id
WHERE name LIKE '%:_username%'");

$this->id_user = $command->queryScalar();

Answer Source

In Yii2 you can use also a activeQuery Notation this way

  $query = new Query;
      ->where(['like', 'user', $this->username );

  $command = $query->createCommand();

  $this->id_user = $command->queryScalar(); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download