Hi i am new to ZF2 and not familiar with some of the changes in ZF2. I would like to know how can i execute SQL query directly from Controller.
I have the following code:
public function indexAction()
{
$db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$sql = "SELECT * FROM books";
$statement = $db->query($sql);
$res = $statement->execute();
if($res instanceof ResultInterface && $res->isQueryResult()){
$resultSet = new ResultSet;
$resultSet->initialize($res);
foreach($resultSet as $row){
echo $row->title . PHP_EOL;
}
}
exit;
/*
return new ViewModel(array(
'books' => $this->getBooksTable()->fetchAll(),
));
*/
}
When the controller above is opened in web browser, it does not show anything. If i echo "Blahh.." before the if statement, it displays the "Blahh.." text correctly.
Does anyone know why it does not display the query result properly? Thx.