2017-04-03 52 views
0

这个代码来从我的数据库中的所有用户名和打印的网页上,但我从这一行symfony的通知:未定义指数:用户名

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'); 
    } 
} 

我的实体的用户文件

<?php 
namespace AppBundle\Entity; 

use FOS\UserBundle\Model\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="`user`") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    /** 
    *@var integer 
    * 
    * @ORM\Column(name="MatchP",type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $MatchP; 
    public function getMatchP() 
    { 
     return $this->MatchP; 
    } 

    public function setMatchP($MatchP) 
    { 
     $this->MatchP = $MatchP; 
    } 

    public function __construct() 
    { 
     parent::__construct(); 
    } 
} 

print_r($row);输出

Array ([0] => Array ([username] => shar) [1] => Array ([username] => koko)) Array ([0] => Array ([username] => shar) [1] 

=>阵列([用户名] =>科科))

+0

'print_r($ row);' – nogad

+0

您能否显示您的'user'实体代码? –

+0

但是'User'实体没有'username'。所以你的查询将是空的。你的意思是不同的实体吗? –

回答

0

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

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

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

你可以试试看看是否有效。我不确定它会起作用。

+0

我得到这个eror调用成员函数getUsername()在数组 – mrsharko

+0

你可以编辑你的文章并显示'print_r($ row);'输出请吗? –

+0

我把它添加到我的文章 – mrsharko