Answer the question
In order to leave comments, you need to log in
What's wrong with connecting to a database in ZendFramewor 2?
Actually, the whole code, as a result, when I write var_dump($this->news) in the view, I get NULL, i.e. the request returned NULL, the database is configured, the scripts seem to be the same, which I don’t understand.
Code in global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=test_task;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
return array(
'db' => array(
'username' => 'root',
'password' => '',
),
);
public function indexAction()
{
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$news = new NewsTable($adapter);
return array(
'news' => $news->fetchAll(),
);
}
...
use Zend\Db\Sql\Sql;
class NewsTable
{
private $sql;
public function __construct($adapter)
{
$this->sql = $adapter;
}
public function fetchAll()
{
$resultSet = $this->sql->query('SELECT * FROM news ORDER BY news_id');
foreach($resultSet as $k=>$v){
$res[$k]=$v;
}
return $res;
}
...
}
Answer the question
In order to leave comments, you need to log in
$st = $this->sql->query('SELECT * FROM news ORDER BY news_id');
$resultSet = $st->execute();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question