PHP CI 實戰 - DB 篇
現在開始不只開發前端html或jquery,只串後端api
開始要直接連資料庫,還是連SQL Server,遇到好多未解的狀況,先公佈一下雷區,
但我還未實戰成功
在Windows這邊,比較麻煩,
database 的設定檔需要設定dbdriver,就是$db['default'][''dbdriver] ,在Linux設定'mssql',這也是預設值,但在Windows上,要設定'sqlsrv'
但Windows要,特別注意÷
先檢查,是否有支援sqlsrv,
使用phpinfo(),再查詢看是否有sqlsrv的區塊,
若沒有,
則需要去microsoft,去下載driver,這部分我還未實驗成功,
英文先參考 https://futbolsalas15.wordpress.com/2014/02/23/7-steps-to-make-sql-server-and-codeigniter-works/
中文參考http://releasebug.blogspot.tw/2015/03/phpci-mssqlapache.html
因為本人英文很弱,所以直接幫大家翻譯一下
要下載哪個MSSQL的Driver呢?
首先要先知道你的PHP版本是多少,再決定要下載哪個版本
我的PHP是5.6
所以參考
Version support for PHP is as follows
Version 4.0 supports PHP 7.0+ on Windows and Linux
Version 3.2 supports PHP 5.6, 5.5, and 5.4 on Windows
Version 3.1 supports PHP 5.5 and 5.4 on Windows
Version 3.0 supports PHP 5.4 on Windows
5.6要下載,Version 3.2版
當你到這個頁面時
https://www.microsoft.com/en-us/download/details.aspx?id=20098
請下載
SQLSRV32.EXE
解壓縮後
會有一堆的檔案,該選擇哪一個檔案好呢?
像我PHP是5.6版
所以要找的檔案是
php_pdo_sqlsrv_56_ts.dll 及 php_pdo_sqlsrv_56_nts.dll ,這是要使用mssql_pconnect使用
php_sqlsrv_56_ts.dll 及 php_sqlsrv_56_nts.dll ,這是要使用mssql_connect使用
這個跟database的設定有關
$db['default']['pconnect'] = TRUE; TRUE就會使用php_pdo_sqlsrv_xxx_xxx.dll
再來是nts、跟ts的說明 (Thread Safe or Non – Thread Safe)
要查一下你的phpinfo(), Thread Safety 的設定,如是enabled,就會使用xxxxxxx_ts.dll
--------
再來,最麻煩的,就是要設定php.ini吧,
我使用的是XAMPP,就要去找你php.ini的位置
沒錯的話,應該會是在
C:\xampp\php
開啟php.ini 開啟,加入extension=php_sql_srv_xxx_xxx.dll
但可能需要加在有[MSSQL]底下
如下
[MSSQL]
mssql.allow_persistent=On
mssql.max_persistent=-1
mssql.max_links=-1
mssql.min_error_severity=10
mssql.min_message_severity=10
mssql.compatability_mode=Off
mssql.secure_connection=Off
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
extension=php_mssql.dll
解除註解
extension_dir="C:\xampp\php\ext"
以下弄完,仍不可行
我出現以下錯誤
Unable to connect to your database server using the provided settings.
Filename: C:/xampp/htdocs/tristarCMS/system/database/DB_driver.php
Line Number: 436
用原生語法測試如下
$serverName = "localhost"; //serverName\instanceName
$connectionInfo = array( "Database"=>"YourDatabaseName", "UID"=>"sa", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
會出現,看不太懂的錯誤訊息
Connection could not be established.
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC �X�ʵ{���z��] �䤣���ƨӷ��W�٥B�����w�w�]���X�ʵ{�� [message] => [Microsoft][ODBC �X�ʵ{���z��] �䤣���ƨӷ��W�٥B�����w�w�]���X�ʵ{�� ) )
這時候,還需要在你Local安裝 ODBC 11 driver
https://www.microsoft.com/en-us/download/confirmation.aspx?id=36434
安裝下載msodbcsql.msi後,就成功過關
恭禧你,經過了最克難的一關,終於可以開始連資料庫了(我是這樣過來的,好累)