簡體   English   中英

PHP MSSQL數據庫Azure

[英]PHP MSSQL Database Azure

我正在嘗試使用PHP將來自Azure的SQL數據庫中的數據解析為JSON。 我有一個免費的虛擬主機服務器上的PHP腳本。 當我連接到Azure的SQL數據庫時收到錯誤消息。

我的PHP腳本

<?php

 $serverName = "tcp:ss4rda587x.database.windows.net,1433";
 $connectionOptions = array("Database"=>"DistribuireColete",
 "Uid"=>"danielcocos26@ss4rda587x", "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. 找出您的免費虛擬主機提供商的公共IP(您的PHP腳本用來撥打電話的公共IP。免費虛擬主機提供商的公共IP意味着此IP很可能會不定期地更改。
  2. 檢查Azure SQL數據庫防火牆規則和
  3. 讓免費的虛擬主機提供商的公共IP通過Azure SQL數據庫防火牆

一個小建議-最好使用免費的Azure 網站 Web應用程序( http://azure.microsoft.com/zh-cn/services/app-service/web/ ),而不要使用免費的虛擬主機提供商。 至少您在配置Azure SQL數據庫防火牆方面不會有問題。

我同意astaykov,請通過Azure門戶設置“允許的ip地址”(您的免費網絡托管提供商的公共ip):

在此處輸入圖片說明

否則,您的Web應用程序應在頁面上打印“ Nu merge”。 另外,我對您的代碼片段有些困惑:

$行= sqlsrv_fetch_array($ stmt,SQLSRV_FETCH_ASSOC)

您可能還想將$stmt更改為$getProducts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM