本文实例讲述了zend框架实现支持sql server的操作方法。分享给大家供大家参考,具体如下:
1.修改Zend/Db/Adapter/Pdo/Abstract.php中的connect方法
protected function _connect() { // if we already have a PDO object, no need to re-connect. if ($this->_connection) { return; } // get the dsn first, because some adapters alter the $_pdoType $dsn = $this->_dsn(); // check for PDO extension if (!extension_loaded('pdo')) { /** * [url=home.php"sqlsrv:Server=".$this->_config['host'].";Database = ".$this->_config['dbname'], $this->_config['username'], $this->_config['password']); $this->_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $this->_connection->setAttribute( PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8 ); $this->_profiler->queryEnd($q); }elseif ($this->_config['pdoType']=='dblib') { $this->_connection = new PDO( $dsn, $this->_config['username'], $this->_config['password'], $this->_config['driver_options'] ); $this->_profiler->queryEnd($q); } // set the PDO connection to perform case-folding on array keys, or not $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding); // always use exceptions. $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception($e->getMessage()); } }
这里针对linux和windows提供两种连接方式。
2.mssql.php 中的为 protected $_pdoType = 'sqlsrv';
protected function _dsn() { // baseline of DSN parts $dsn = $this->_config; // don't pass the username and password in the DSN unset($dsn['username']); unset($dsn['password']); unset($dsn['driver_options']); if (isset($dsn['port'])) { $seperator = ':'; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $seperator = ','; } $dsn['host'] .= $seperator . $dsn['port']; unset($dsn['port']); } // this driver supports multiple DSN prefixes // @see http://www.php.net/manual/en/ref.pdo-dblib.connection.php //print_r($dsn);exit; if (isset($dsn['pdoType'])) { switch (strtolower($dsn['pdoType'])) { case 'freetds': case 'sybase': $this->_pdoType = 'sybase'; break; case 'mssql': $this->_pdoType = 'mssql'; break; case 'sqlsrv': $this->_pdoType = 'sqlsrv'; break; case 'dblib': default: $this->_pdoType = 'dblib'; break; } unset($dsn['pdoType']); } // use all remaining parts in the DSN foreach ($dsn as $key => $val) { $dsn[$key] = "$key=$val"; } $dsn = $this->_pdoType . ':' . implode(';', $dsn); // print_r($dsn);exit; return $dsn; }
3.ZF 的web.xml 数据库配置文件改成:
<db> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <username>sa</username> <password>123456</password> <dbname>testdb </dbname> <pdoType>sqlsrv</pdoType> </config> </db>
期间遇到中文乱码问题
function convert2utf8($string) { $config = $this->getCfg(); $pdoType = $config->db->config->pdoType; if($pdoType == 'dblib'){ return iconv("gbk","utf-8",$string); }elseif($pdoType == 'sqlsrv'){ return mb_convert_encoding($string,"UTF-8","auto"); } } function convert2gbk($string) { $config = $this->getCfg(); $pdoType = $config->db->config->pdoType; if($pdoType == 'dblib'){ return iconv("utf-8","gbk",$string); }elseif($pdoType == 'sqlsrv'){ return mb_convert_encoding($string,"GBK","auto"); } } protected function &getCfg() { if ($this->cfg_ === null) { $registry = Zend_Registry::getInstance(); $this->cfg_ = $registry->get('web_config'); } return $this->cfg_; }
针对不同的类型,进行不同的处理。
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
标签:
zend,sql,server
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“zend框架实现支持sql server的操作方法”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。