2015-06-21 43 views
0

我想從SQL數據庫從Azure解析數據到使用PHP的JSON。我有一個免費的網絡託管服務器上的PHP腳本。當我連接到Azure的SQL數據庫時,我收到了一個錯誤。PHP的MSSQL數據庫Azure

我的PHP腳本

<?php 

$serverName = "tcp:ss4rda587x.database.windows.net,1433"; 
$connectionOptions = array("Database"=>"DistribuireColete", 
"Uid"=>"[email protected]", "PWD"=>"******"); 

//Establishes the connection 
$conn = sqlsrv_connect($serverName, $connectionOptions); 

//Select Query 
$tsql = "SELECT * FROM Clienti"; 

//Executes the query 
$getProducts = sqlsrv_query($conn, $tsql); 

     if (!$getProducts) 
     { 
      //Query failed 
      echo("Nu merge"); 
     } 

     else 
     { 
      $clienti = array(); //Create an array to hold all of the contacts 
      //Query successful, begin putting each contact into an array of contacts 

      while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) //While there are still contacts 
      { 
       //Create an associative array to hold the current contact 
       //the names must match exactly the property names in the contact class in our C# code. 
       $client = array("ID" => $row['IdClient'], 
           "Name" => $row['NumeClient'], 
           "Number" => $row['TelNumar'], 
           "ImageBase64" => base64_encode($row['Icon']) 
           ); 

       //Add the contact to the contacts array 
       array_push($clienti, $client); 
      } 

      //Echo out the contacts array in JSON format 
      echo json_encode($clienti); 
     } 
?> 

和錯誤我收到

Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given in H:\root\home\cdan26-001\www\site1\GetClienti.php on line 14 

回答

1

您需要:

  1. 找出你的免費網絡的公網IP託管提供者(您的P的公共IP HP腳本用於撥打電話。這與免費的網絡託管服務提供商意味着這個IP很可能會不定期變化。
  2. 檢查Azure SQL Database Firewall規則和
  3. Let the public IP of the free web hosting provider through your Azure SQL Database Firewall

一個小的建議 - 你最好使用Azure的網站 Web應用程序(http://azure.microsoft.com/en-us/services/app-service/web/)的自由層,而不是免費的網絡託管服務提供商。至少您將不會遇到配置Azure SQL數據庫防火牆的問題。

1

我astaykov同意,請設置「允許的IP地址」(您的免費網站託管服務提供商的公網IP)通過Azure的門戶網站:

enter image description here

否則你的web應用程序應打印「怒江合併」上這一頁。另外,我在你的代碼片段有點糊塗了:

$行= sqlsrv_fetch_array($語句,SQLSRV_FETCH_ASSOC)

您可能還需要改變 $stmt$getProducts

相關問題