zend框架實(shí)現(xiàn)支持sql server的操作方法
本文實(shí)例講述了zend框架實(shí)現(xiàn)支持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?mod=space&uid=86763]@see[/url] Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded'); } // check the PDO driver is available if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed'); } // create PDO connection $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT); // add the persistence flag if we find it in our config array if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) { $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true; } try { //print_r($this->_config);exit; if($this->_config['pdoType']=='sqlsrv'){ $this->_connection = new PDO( "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 數(shù)據(jù)庫配置文件改成:
<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_; }
針對不同的類型,進(jìn)行不同的處理。
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- Zend Framework框架之Zend_Mail實(shí)現(xiàn)發(fā)送Email郵件驗(yàn)證功能及解決標(biāo)題亂碼的方法
- Zend Framework入門教程之Zend_Db數(shù)據(jù)庫操作詳解
- ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫的方法
- Zend Framework開發(fā)入門經(jīng)典教程
- Zend Framework教程之Zend_Config_Ini用法分析
- Zend Framework教程之Zend_Config_Xml用法分析
- Zend Framework教程之Zend_Registry對象用法分析
- Zend Framework框架Smarty擴(kuò)展實(shí)現(xiàn)方法
- Zend Framework框架路由機(jī)制代碼分析
- Zend Framework入門教程之Zend_Mail用法示例
相關(guān)文章
實(shí)現(xiàn)PHP+Mysql無限分類的方法匯總
這篇文章主要給大家匯總介紹了實(shí)現(xiàn)PHP+Mysql無限分類的2種方法,并對比分析了2種方法的優(yōu)劣,需要的朋友可以參考下2015-03-03PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程
php5.3或以上,且為線程安全版本。apache和php使用的編譯器必須一致,通過phpinfo()查看Thread Safety為enabled則為線程安全版,通過phpinfo()查看Compiler項(xiàng)可以知道使用的編譯器,本文給大家介紹PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程,需要的朋友參考下2015-11-11Laravel 實(shí)現(xiàn)在Blade模版中使用全局變量代替路徑的例子
今天小編就為大家分享一篇Laravel 實(shí)現(xiàn)在Blade模版中使用全局變量代替路徑的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題
PHP CURL與file_get_contents函數(shù)都可以獲取遠(yuǎn)程服務(wù)器上的文件保存到本地,但在性能上面兩者完全不在同一個(gè)級(jí)別,下面通過一個(gè)例子給大家介紹PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題,需要的朋友參考下2015-11-11php 問卷調(diào)查結(jié)果統(tǒng)計(jì)
一個(gè)新產(chǎn)品投入市場,要先做問卷調(diào)查,考察這個(gè)產(chǎn)品在市場的需求量,本篇文章主要跟大家介紹使用php問卷調(diào)查結(jié)果統(tǒng)計(jì),感興趣的朋友一起學(xué)習(xí)吧2015-10-10