2014-10-07 24 views
0

我有一個問題,我怎麼能從我的數據庫(Microsoft SQL Server)從我的JavaScript中通過AJAX請求選擇數據。 我知道我需要一個「服務器語言」,但似乎PHP無法做到這一點!Javascript到Microsoft SQL Server(AJAX請求)

我該怎麼辦?

謝謝!

+0

這是一個基於PHP的問題或asp.net?因爲你已經給了php和asp.net的標籤 – Manu 2014-10-07 06:40:39

+0

當然,你可以在PHP中這樣做:http://php.net/manual/en/book.mssql.php – 2014-10-07 06:42:26

+0

謝謝你的回答,這是兩個!事實上,我不知道是否可以在php或asp.net中完成(asp.net中的極端初學者,我從來沒有碰過這個!) – 2014-10-07 06:42:56

回答

1

PHP是一種服務器端語言。驅動程序是爲PHP包創建的,它允許它們與幾種不同類型的數據庫體系結構系統進行交互。在這種情況下,SQL Server將通過sqlsrv驅動程序連接到PHP。

一個簡單的數據庫查詢如下所示:

-- query.php -- 

$serverName = "serverName\sqlexpress"; 
$connectionInfo = array("Database"=>"dbName", "UID"=>"username", "PWD"=>"password"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 
if($conn === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

$sql = "SELECT * FROM Person"; 

$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

if(sqlsrv_fetch($stmt) === false) { 
    die(print_r(sqlsrv_errors(), true)); 
} 

$name = sqlsrv_get_field($stmt, 0); 
echo $name; //maybe the name is "George" 

此建立連接,然後嘗試查詢數據庫。由於我們只是檢索一行,我們使用sqlsrv_fetch()來嘗試填充$stmt變量。如果它起作用,那麼我們將得到$name作爲索引爲0的列中的行的返回。這將在$name值返回給我們ajax callsuccess function(如下圖所示)

$.ajax()很簡單。弄清楚什麼元素會解僱Ajax調用,那麼就去做吧..

$('element').on('click', function(e){ 
    e.preventDefault(); 
    $.ajax({ 
     url: 'query.php', 
     type: 'GET', 
     success: function(data){ 
      console.log(data); //will show George in the console 
      //otherwise it will show sql_srv errors. 
     } 
    }); 
}); 

資源

  1. sqlsrv_connect()
  2. sqlsrv_query()
  3. $.ajax()
0

對於連接到SQL Server ...你c使用此代碼...

public function connect() { 
    $dsn = "Driver={SQL Server};Server=xxxxx;Port=1433;Database=yyyy"; 
    $data_source='zzzz'; 
    $user='dbadmin'; 
    $password='password'; 
    // Connect to the data source and get a handle for that connection. 
    $conn=odbc_connect($dsn,$user,$password); 
    if (!$conn) { 
     if (phpversion() < '4.0') 
     { 
       exit("Connection Failed: . $php_errormsg"); 
      } 
     else 
     { 
       exit("Connection Failed:" . odbc_errormsg()); 
     } 
    } 
    return $conn; 
} 

請注意,在這裏我創建了一個數據源。正如你所看到的,這段代碼正在使用ODBC。 ;) 而這個連接使用Sql Authentication。 希望這有助於...

0

Asp.net

客戶端代碼

$.ajax({ 
      url: "ViewData.aspx/GetTransitNameById", 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      data: '{"Transitid":"' + id + '"}', 
      success: function (result) { 
      // You got the required data in result.d 
      var finalresult = result.d; 
      } 
     }); 

服務器端代碼

[WebMethod] 
    public static string GetTransitNameById(int Transitid) 
    { 
     string name = ""; 
     try 
     { 
      oohMonitoringManager om = new oohMonitoringManager(); 
      name = om.GetTransitNameByTransitId(Transitid); 
      // here you got what you needed. 
      // name contains the data that you have to return back to the javascript ajax function 

     } 
     catch (Exception a1) 
     { } 

     return name; 
    }