Php symfony通知:未定义索引:用户名

Php symfony通知:未定义索引:用户名,php,mysql,database,symfony,Php,Mysql,Database,Symfony,这段代码用于从我的数据库中获取所有用户名并将其打印到网页上,但我从这一行中得到此错误 echo $row['username']; 怎么了 <?php namespace test\stuffBundle\Controller; use tuto\testBundle\Entity\Users; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\DependencyInjection\Cont

这段代码用于从我的数据库中获取所有用户名并将其打印到网页上,但我从这一行中得到此错误

echo $row['username'];
怎么了

<?php

namespace test\stuffBundle\Controller;


use tuto\testBundle\Entity\Users;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Session\Session;
use Ratchet\WebSocket\WsServerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {       


        $conn = $this->get('database_connection');

        while ($row = $conn->fetchAll('SELECT username FROM user')) {
            echo $row['username'];
        }
        return $this->render('teststuffBundle:Default:index.html.twig');
    }
}
=>数组([username]=>koko))


我怀疑这是一个结果集,您需要这样尝试:

$row = $conn->fetchAll('SELECT username FROM user');

foreach( $row as $user){
    echo $user['username'];
}

你能试试看这是否管用吗。但我不确定它是否会起作用。

print\r($row)
能否显示您的
用户
实体代码?但是
用户
实体没有
用户名
。因此,您的查询将是空的。你是说一个不同的实体吗?@AlvinBunk那是fosuserbundle实体我试着在会话中使用它来编辑一些用户信息,但当我使用echo not working时,我试着使用echo$row['MatchP'],我得到了相同的错误我得到了对成员函数getUsername()的这个eror调用在Array上,您可以编辑帖子并显示打印内容($row)输出?我把它添加到我的帖子里了
Array ( [0] => Array ( [username] => shar ) [1] => Array ( [username] => koko ) ) Array ( [0] => Array ( [username] => shar ) [1]
$row = $conn->fetchAll('SELECT username FROM user');

foreach( $row as $user){
    echo $user['username'];
}