【问题标题】:PHP MSSQL Database AzurePHP MSSQL 数据库 Azure
【发布时间】:2015-06-21 23:45:41
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: php sql-server json azure


    【解决方案1】:

    你需要:

    1. 找出您的免费网络托管服务提供商的公共 IP(您的 PHP 脚本用来进行调用的公共 IP。而对于免费网络托管服务提供商而言,这意味着该 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 数据库防火墙不会有问题。

    【讨论】:

      【解决方案2】:

      我同意 astaykov,请通过 Azure 门户设置“允许的 ip 地址”(您的免费网络托管服务提供商的公共 ip):

      否则您的网络应用程序应在页面上打印“Nu merge”。另外,我对你的代码sn-p有点困惑:

      $row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)

      您可能还想将$stmt 更改为$getProducts

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-12
        • 2018-08-01
        • 2015-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多