Zend Framework 连接 MSSQL数据库遇到的问题及解决过程

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/shumago/article/details/52638331

依据网站上查到的资料连接MSSQL数据库的步骤如下

注:zend framework通常需要php5.3以上的版本,但php5.3以上版本已经不具备对pdo_mssql数据库的支持,需要手动下载微软的数据库驱动,具体操作方法参见

http://blog.csdn.net/sptoor/article/details/7704991

1.configs目录的application.ini中添加以下内容


[mssql]
db.adapter=PDO_MSSQL
db.params.host=localhost
db.params.username=sa
db.params.password=****
db.params.dbname=****


2. 连接代码信息可以添加至控制器的init函数中

$url=constant("APPLICATION_PATH").DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini';
$dbconfig=new Zend_Config_Ini($url,"mssql");
$db=Zend_Db::factory($dbconfig->db);
var_dump($db);
$db->query("SET NAMES UTF-8");
Zend_Db_Table::setDefaultAdapter($db);
$res = $db->fetchAll("select * from tablename");
echo print_r($res);

依据操作后无法连接数据库, zend 测试页面无法正常显示

错误信息如下:

[23-Sep-2016 14:55:02] PHP Fatal error:  Uncaught exception 'Zend_Db_Adapter_Exception' with message 'The mssql driver is not currently installed' in D:\WWWRoot\test\library\Zend\Db\Adapter\Pdo\Abstract.php:112
Stack trace:
#0 D:\WWWRoot\test\library\Zend\Db\Adapter\Pdo\Mssql.php(137): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 D:\WWWRoot\test\library\Zend\Db\Adapter\Abstract.php(459): Zend_Db_Adapter_Pdo_Mssql->_connect()
#2 D:\WWWRoot\test\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('select * from k...', Array)
#3 D:\WWWRoot\test\library\Zend\Db\Adapter\Abstract.php(734): Zend_Db_Adapter_Pdo_Abstract->query('select * from k...', Array)
#4 D:\WWWRoot\test\application\Bootstrap.php(17): Zend_Db_Adapter_Abstract->fetchAll('select * from k...')
#5 D:\WWWRoot\test\library\Zend\Application.php(325): Bootstrap->__construct(Object(Zend_Application))
#6 D:\WWWRoot\test\library\Zend\Application.php(184): Zend_Application->setBootstrap('D:\WWWRoot\test...', 'Bootstrap')
#7 D:\WWWRoot\test\library\Zend\Application.php(92):  in D:\WWWRoot\test\library\Zend\Db\Adapter\Pdo\Abstract.php on line 112

错误信息是通过修改php.ini文件输出error日志查到的,测试页面不能显示错误信息

查看phpinfo() 查看


最后经过若干尝试将连接信息修改为:测试后通过。可以正常访问MSSQL数据库。

[mssql]
db.adapter=Sqlsrv
db.params.host=localhost
db.params.username=sa
db.params.password=****
db.params.dbname=****

展开阅读全文

Zend Framework 事务处理 问题

08-30

官方给出一个示例,如下:rn[code=PHP]beginTransaction();rnrn// 尝试数据库操作.rn// 假如成功,commit该操作;rn// 假如, roll back.rntry rn $db->query(...);rn $db->commit();rn catch (Exception $e) rn $db->rollBack();rn echo $e->getMessage();rnrnrn?>[/code]rnrn我写的实例中,确没有通过,而是提示rnFatal error: Uncaught exception 'PDOException' with message 'There is already an active transaction' in E:\php\ZendFramework\library\Zend\Db\Adapter\Pdo\Abstract.php:305 Stack trace: #0 E:\php\ZendFramework\library\Zend\Db\Adapter\Pdo\Abstract.php(305): PDO->beginTransaction() #1 E:\php\ZendFramework\library\Zend\Db\Adapter\Abstract.php(484): Zend_Db_Adapter_Pdo_Abstract->_beginTransaction() #2 D:\svnrepos\WiClub\src\application\models\shop.php(76): Zend_Db_Adapter_Abstract->beginTransaction() #3 D:\svnrepos\WiClub\src\application\controllers\ShopController.php(507): shop->inserttest(Array) #4 E:\php\ZendFramework\library\Zend\Controller\Action.php(513): ShopController->shopaddtestAction() #5 E:\php\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('shopaddtestActi...') #6 E:\php\ZendFramework\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #7 D:\svnrepos\WiC in E:\php\ZendFramework\library\Zend\Db\Adapter\Pdo\Abstract.php on line 305rnrn似乎是提示已经有一个事务在运行?刚接触ZF不久,不太熟悉,把相关源文件贴上来吧rnrnconfig.inirn[code=PHP]rn[general]rndb.adapter = PDO_MYSQLrndb.config.host = localhostrndb.config.username = rootrndb.config.password = 123456rndb.config.dbname = testdb rndb.config.charset = utf8rndate_default_timezone = "PRC"rn[/code]rnrnUtil.php [所在文件夹 Common,与controllers文件夹,models文件夹同级]rn[code=PHP]rnset('config',$config);rn $params=$config->db->config->toArray();rn $params['options']=array(PDO::ATTR_PERSISTENT => true);rn $db = Zend_Db::factory($config->db->adapter,$params);rn if($setcharset)rn rn $charset =($params['charset']);rn $db->query("set names $charset;");rn rn return $db;rn rn elsern rn return false;rn rn rnrn[/code]rnrnShopController.php [所在文件夹 controllers]rn[code=PHP]rn_db=Common_Util::getDb("general",true); //实例化数据库字符串连接rn $this->view->baseUrl = $this->_request->getBaseUrl();rn $this->defaultNs = new Zend_Session_Namespace('Default');rn rn rnrn public function shopaddtestAction()rn rn $dds=new shop("shop_type",$this->_db);rn for($i=2147483645;$i<2147683650;$i++)rn rn $data=array("shop_id"=>$i,"type"=>$i);rn $dds->inserttest($data);rn rn rn rnrn[/code]rnrnShop.php [所在文件夹 models]rn[code=PHP]rn_db=$db;rn rn if($name)rn rn $this->_name=$name;rn rn rn rn public function inserttest($data)rn rn if($data)rn rn $this->_db->beginTransaction();rn tryrn rn $rs = $this->_db->insert($this->_name,$data);rn if($rs)rn rn $rs = $this->_db->lastInsertId();rn rn return $rs;rn rn $this->_db->commit();rn rn catch(Exception $e)rn rn $this->_db->rollback(); error_log($e->getMessage()); rn rn rn return false; rn rnrn[/code]rnrn请各位大牛支招,谢谢! 论坛

没有更多推荐了,返回首页