thinkphp5連線oracle資料庫
阿新 • • 發佈:2018-12-21
根據目前thinkphp開發多數使用的是mysqsl資料庫,很少有使用oracle資料庫的,目前有需求需要使用oracle資料庫。
通過查閱別人的部落格,踩了很多坑終於找到一個可以連結oralce資料庫的方法。
需要使用到think-oracle。
首先需要下載oracle客戶端,然後配置oci,修改php.ini檔案。
1,Oracle官方網站下載安裝Oracle客戶端。
下載地址:
http://www.oracle.com/technetwork/cn/database/features/instant-client/index-097480.html
選擇一個合適的版本,下載,**然後將解壓後的目錄新增到系統變數(重要)**
2,下載PHP 擴充套件Oracle客戶端 DLL連結檔案。
http://pecl.php.net/package/oci8 選擇DLL下載;
將下載的檔案接下dll 檔案到 PHP的DLL擴充套件目錄
3,php.ini 配置檔案中開啟 Oracle連線擴充套件。
extension=php_pdo_oci.dll
extension=php_oci8.dll
- 開啟phpinfo檢視是否有oci8的相關資訊,如果有則說明 開啟拓展成功了,沒有的話,需要自己費點時間看一下原因
二、下載ThinkPHP Oracle資料庫擴充套件驅動類;
1,Oracle資料庫擴充套件驅動類下載地址。
https://github.com/top-think/think-oracle
這裡特別說明一下,5.0版本的請下載v1.*版本的think-oracle,5.1版本的請下載v2.*版本的think-oracle,不然會出現相容性問題
將下載好的檔案解壓,會得到src目錄下的兩個檔案,Builder.php和Connection.php,(這裡我以5.0版本為例,我下載的是v1.3版本的)
(1)將Builder.php和Connection.php分別放到:\thinkphp\library\think\db對應的builder和connector目錄並都改名為Oracle.php;
(2)修改builder目錄下的Oracle.php檔案,如下:
namespace think\oracle;
use think\db\Builder as BaseBuilder;
use think\db\Query;
/**
* Oracle資料庫驅動
*/
class Builder extends BaseBuilder
改為
namespace think\db\builder;
use think\db\Builder;
use think\Exception;
/**
* Oracle資料庫驅動
*/
class Oracle extends Builder
(3)修改connector目錄下的Oracle.php檔案,如下:
namespace think\oracle;
use PDO;
use think\db\Connection as BaseConnection;
/**
* Oracle資料庫驅動
*/
class Connection extends BaseConnection
改為
namespace think\db\connector;
use PDO;
use think\db\Connection;
use think\Log;
/**
* Oracle資料庫驅動
*/
class Oracle extends Connection
(4)配置Oracle連線引數,如下:
return [
// 資料庫型別
'type' => 'oracle',
// 伺服器地址
'hostname' => '127.0.0.1',
// 資料庫名
'database' => 'orcl',
// 使用者名稱
'username' => 'test',
// 密碼
'password' => '你的Oracle資料庫密碼',
// 埠
'hostport' => '1521',
// 連線dsn
'dsn' => '',
// 資料庫連線引數
'params' => [],
// 資料庫編碼預設採用utf8
'charset' => 'utf8',
// 資料庫表字首
'prefix' => '',
// 資料庫除錯模式
'debug' => true,
// 資料庫部署方式:0 集中式(單一伺服器),1 分散式(主從伺服器)
'deploy' => 0,
// 資料庫讀寫是否分離 主從式有效
'rw_separate' => false,
// 讀寫分離後 主伺服器數量
'master_num' => 1,
// 指定從伺服器序號
'slave_no' => '',
// 是否嚴格檢查欄位是否存在
'fields_strict' => true,
// 資料集返回型別
'resultset_type' => 'array',
// 自動寫入時間戳欄位
'auto_timestamp' => false,
// 時間欄位取出後的預設時間格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要進行SQL效能分析
'sql_explain' => false,
];
後面就可以自己連線資料庫輸出資料了;