繁体   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