此代码从我的数据库中获取所有用户名并将其打印在网页上,但我从此行中收到此错误
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]
=&GT;数组([用户名] =&gt; koko))
答案 0 :(得分:1)
我怀疑这是一个结果集,你需要尝试这样:
$row = $conn->fetchAll('SELECT username FROM user');
foreach( $row as $user){
echo $user['username'];
}
你能试试看看是否有效。我不确定它会起作用。