php - Display data from database using html encode(yii2)

Solution:

you can encode the single column result and ->all() return a collection fo models

so first you should access to single model, eg: assuming you obtain your collection of models as an array

    <?php $model = ExampleModule::find()->select('anycolumn')->asArray->()all(); ?>

you can encode the single column result for the first model this way

    <?= Html::encode($model[0]['your_column']); ?>

Answer

Solution:

Get all data first .

First step

<?php $model = ExampleModule::find()->select('anycolumn')->asArray()->all(); ?>

Second step

<?= Html::encode($model[0]['anycolumn']); ?>

Answer

Solution:

First of all ExampleModule::find()->select('anycolumn')->all() returns an array of records.

If you want to get first found record you need to use

<?php
$model = ExampleModule::find()->select('anycolumn')->one();
?>

Then

<?= Html::encode($model->anycolumn) ?>

Or if you want to display all records:

<?php
foreach (ExampleModule::find()->select('anycolumn')->all() as $model) {
    echo Html::encode($model->anycolumn) . '<br>';
}
?>

Source