2015-09-17 14 views
0

我想連接PHP與SQL Server 2008,但我無法建立連接。在Windows10中與SQL Server 2008的PHP連接

我跟着以下教程: https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx

我的php.ini

enter image description here

我的DLL和地點:

enter image description here

我的PHP版本: enter image description here

我的分機都出現在WAMP PHP擴展:

enter image description here

但是當我嘗試下面的PHP代碼:

<?php 
$serverName = "SAPSRV"; //serverName\instanceName 

// Since UID and PWD are not specified in the $connectionInfo array, 
// The connection will be attempted using Windows Authentication. 
$connectionInfo = array("Database"=>"SmartLogistic"); 
$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)); 
} 
?> 

我仍然得到以下錯誤:

(!) Fatal error: Call to undefined function sqlsrv_connect() in H:\wamp\www\job\sql.php

幫我解決這個問題,在此先感謝。 。

+0

@PaulCrovella我想確保我沒有寄託都正確..所以讓更多的選擇給他人使用圖像找到我的錯也不錯,這樣只有我試過.. – TomPHP

回答

2

幾個月前,我有完全相同的錯誤,這是什麼爲我工作。

首先爲sqlsrv驅動程序工作,你將需要32位WAMP服務器它不能與64位WAMP一起使用。

我使用php_sqlsrv_55_ts DLL文件,我可以在WAMP中的PHP中運行我的查詢。

現在,這裏是它變得有趣。我假設你將在基於Linux的服務器上運行你的系統?

如果是這種情況sqlsrv不能在基於Linux的操作系統上工作,所以您將不得不在Linux操作系統上使用mssql驅動程序。 Here is a link進行設置。

最後,您需要編寫一個自定義函數來檢查您的PHP擴展,然後根據您運行它們的位置選擇正確的驅動程序。

這是我創建的一個函數,用於查看哪個驅動器已加載並確定使用哪個驅動器。

// Function to check which MS SQL database driver is loaded 
function get_db_loaded_extension() { 
    // Assign php loaded extensions to an array variable 
    $php_loaded_extensions_array = get_loaded_extensions(); 

    // Loop through each php extension in the array 
    foreach($php_loaded_extensions_array as $php_ext) { 
     // Switch to check which MS SQL database driver is loaded 
     switch($php_ext) { 
      case "mssql": 
       $return = "mssql"; 
       break; 

      case "sqlsrv": 
       $return = "sqlsrv"; 
       break; 
     } 
    } 

    // Check if a MS SQL database driver have been found 
    if(!isset($return)) { 
     $return = js_dialog("No Microsoft SQL database driver loaded."); 
    } 

    return $return; 
} 

正如一個例子,這裏也是如何創建連接。

// Call function to check which MS SQL database driver extension is loaded 
$mssql_db_driver = get_db_loaded_extension(); 

// Switch to determine how to make the appropriate connection 
switch($mssql_db_driver) { 
    case "mssql": 
     // Set the MSSQL database variables 
     $mssql_servername = "xxx.xxx.xxx.xxx"; 
     $mssql_username = "my_user"; 
     $mssql_password = "**********"; 

     // Create conection to MSSQL using the mssql php extension 
     $mssql_conn = mssql_connect($mssql_servername, $mssql_username, $mssql_password); 
     break; 

    case "sqlsrv": 
     // Set the MSSQL database variables 
     $mssql_servername = "xxx.xxx.xxx.xxx"; 
     $mssql_conn_info = array(
      "UID" => "my_user", 
      "PWD"=> "**********" 
     ); 

     // Create conection to MSSQL using the sqlsrv php extension 
     $mssql_conn = sqlsrv_connect($mssql_servername, $mssql_conn_info); 
     break; 

    default: 
     echo $mssql_db_driver; 
     break; 
} 

// Check the MSSQL connection 
if(!$mssql_conn) { 
    exit("Could not connect to the database. Please contact the administrator."); 
} 
+0

它工作時,我把WAMP更改爲32位... – TomPHP

0

它看起來像是有一個問題,而不是SQL。 sqlsrv_connect是一個未知的函數,所以它不能被調用。嘗試使用PDO開始。它是與SQL數據庫進行交互的更好,更安全的方式。
http://php.net/manual/en/class.pdo.php

以下應該允許您查詢sql server db。

$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); 
$stmt = $dbh->prepare("SELECT * FROM Table"); 
$stmt->execute(); 
+0

致命錯誤(!):未捕獲在第7行的H:\ wamp \ www \ job \ sql.php中有異常'PDOException'找不到驅動程序' (!)PDOException:在H:\ wamp \ www \ job \ sql中找不到驅動程序。第7行的php – TomPHP

+0

Hasting我嘗試了ur代碼,並收到trhe上面的錯誤。 – TomPHP

+0

您錯過了sql服務器驅動程序。 http://www.microsoft.com/en-IE/download/details.aspx?id=20098 –