Reputation: 1520
I want to list all distinct records from create_client table, and have tried this code but am getting an error.
What's wrong with this code?
$query = new Query;
$ccname = (new yii\db\Query())
->select(['id','company_name','client_code'])
->from('create_client')
->distinct()
->all();
$sidemenus = mysql_fetch_array($ccname);
echo $sidemenu['company_name']."<br />";
Update-
Got the desired result by this-
$posts = $db->createCommand('SELECT DISTINCT company_name,client_code FROM create_client')
->queryAll();
var_dump($posts)
But how to convert this in to string so that I can use on page?
Upvotes: 1
Views: 5906
Reputation: 2788
try your query modified like that
$query = new yii\db\Query();
$data = $query->select(['id','company_name','client_code'])
->from('create_client')
->distinct()
->all();
the important part is all() method because it execute your query and return result as array
all() Executes the query and returns all results as an array.
then you can work with result as normal array
if($data) {
foreach($data as $row)
{
echo 'company_name: ' . $row['company_name'] . ' client_code: ' . $row['client_code'] . '<br>';
}
}
documentation yii2 - class yii\db\Query
Upvotes: 2