1

I have been using azure mobile services for my android application. Then I want to access my database datas with my Localhost and Remote server using Php.

I tried the codes that is given by Azure docs below.

$conn = new PDO ( "sqlsrv:server = tcp:MYSERVERNAME.database.windows.net,1433; Database = MY_DATABASE_NAME", "MY_USER_NAME", "MY_PASSWORD" );
try {
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

} catch ( PDOException $e ) {
    print( "Error connecting to SQL Server." );
    die( print_r( $e ) );

};

When The code is runned on Localhost or Remote server, that is Linux server, I get error below

enter image description here

EDIT

I thought it is about Azure firewall rules and I added my IP number in Azure Firewall rules for Server

When I tried to connect to Server via SQLPro I could connect successfuly and I started to query. But I can not to connect via Localhost with my Php codes.

1
  • The detail about using a Macbook is irrelevant. And which Azure Firewall did you alter: SQL Database server firewall? If so, how did you set it? (127.0.0.1 won't help, as that's a localhost address). You should edit your question with that detail. Commented Mar 15, 2016 at 10:41

3 Answers 3

4

The function you used PDO sqlsrv:server requires SQLSRV extension like php_pdo_sqlsrv_53_nts.dll, but which is only compatible with PHP running on Windows.

To connect to SQL server in PHP in Unix, you can use ODBC extension and Microsoft's SQL Server ODBC Driver for Linux.

You can refer to http://php.net/manual/en/ref.pdo-sqlsrv.php for details.

Sign up to request clarification or add additional context in comments.

1 Comment

You exactly right, I solved my probled by using Azure Web App + SQL service. I use this resource azure.microsoft.com/en-us/documentation/articles/…
2

Another recommendation for connecting to Azure SQL DB from your Linux server is using sqlshim. The sqlshim project aims to replicate Microsoft SQL Server Driver for PHP (sqlsrv) on Linux/OS X.

Here is a link to the project: https://github.com/radsectors/sqlshim

Here is how you would install it:

Download the latest release from: https://github.com/radsectors/sqlshim/releases
Extract src/sqlshim.php and src/globals.php.
Include it.
require 'src/sqlshim.php';

Cheers,
Meet

Comments

-1

Did you add the firewall exceptions to the database or the database instance?

Instance level(Run at master):

EXECUTE sp_set_firewall_rule N'my_rule','123.0.0.1','123.0.0.1'; 
--EXECUTE sp_set_firewall_rule N'rule_name','IP_Range_low','IP_Range_high'; 

Database Level (Run at target DB):

EXECUTE sp_set_database_firewall_rule N'my_db_rule'; 
,'123.0.0.1'  --IP_Range_low
,'123.0.0.1'  --IP_Range_high

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.