简体   繁体   中英

PHP MSSQL Database Azure

I am trying to get parse data from the SQL Database from Azure into a JSON using PHP. I have the php script on a free web hosting server. I recieve an ERROR when I am to connect to the Azure's SQL Database.

My PHP Script

<?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);
        }
?>

And the ERROR I recieve

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

You need to:

  1. Find out the public IP of your free web hosting provider (the public IP that your PHP script uses to make calls. And this with free web hosting provider means that this IP will most likely change on an irregular basis.
  2. Check the Azure SQL Database Firewall rules and
  3. Let the public IP of the free web hosting provider through your Azure SQL Database Firewall .

A small advice - you better use the free tier of Azure Web Sites Web Apps ( http://azure.microsoft.com/en-us/services/app-service/web/ ) , instead of the free web hosting provider. At least you will not have issues with configuring the Azure SQL Database Firewall.

I agree with astaykov, please set up 'allowed ip addresses' (your free web hosting provider's public ip) via Azure portal:

在此处输入图片说明

otherwise your web APP should print 'Nu merge' on the page. In addition, I have a bit confused in your code snippet:

$row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)

you may also want to change $stmt to $getProducts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM