我有ZF2試過,但它沒有給任何查詢結果與ZF1作品,ZF2不工作。 ZF2數據庫適配器是否不完整,留下的錯誤未被ZF2本身解決?原因文件告訴這樣做,但它根本無法正常工作。 ZF2適配器是否可以工作?Zend框架2 - 我如何連接到數據庫並檢索查詢結果?
TestController.php
<?php
namespace Application\Controller;
//namespace Application\Model;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Adapter\Adapter;
use Zend\Debug\Debug;
class TestController extends AbstractActionController {
public function indexAction() {
$sql = "SELECT * FROM stackoverflow";
$statement = $this->adapter->query($sql);
$res = $statement->execute();
Debug::dump($res);
exit;
}
}
Module.php
<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http;
class Module {
public function getServiceConfiguration()
{
return array(
'factories' => array(
'adapter' => function($sm) {
$config = $sm->get('config');
$dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
return $dbAdapter;
}
),
);
}
}
global.php
<?php
return array(
// 'di' =>array(
// 'instance' =>array(
// 'PDO' => array(
// 'parameters' => array(
// 'dsn' => 'mysql:dbname=mydb;host=localhost',
// 'username' => 'mydb',
// 'password' => '',
// )
// ),
//
// 'User' => array(
// 'parameters' => array(
// 'pdo' => 'PDO'
// )
// )
// )
// ),
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'mydb',
'password' => '',
// 'driver_options' => array(
// PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
// ),
),
// 'service_manager' => array(
// 'factories' => array(
// 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
// ),
// ),
);
不解釋你如何得到的結果,而這樣做轉儲,它不會顯示一個記錄。 – YumYumYum