I want to connect with SQL Server using PHP on Azure.
My code works successfully on my host, but the following issues occur after I deploy to Azure application service:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5]
Code:
try {
$conn = new PDO("sqlsrv:Server=1xx.xx.xx.xx;Database=MyDB", "username", "password");
} catch (PDOException $e) {
echo $e->getMessage();
}
1xx.xx.xx.xx is another remote server, I want to connect to this server from Azure.
And phpinfo.php:
PDO drivers : mysql, sqlsrv, pgsql
So I should be able to use the PDO sqlsrv function.
What do I need to set up on the Azure application service?
Before you deploy your PHP code to Azure APP Service, I think you need to test if you PHP code can connect to the remote SQL Server on local computer successfully.
If you want to connect to remote SQL server, you must make sure that:
1. make sure you have allow network connection from sql server configuration tool
2. allow connection for this port in firewall
3. activate sql browser service
4. make sure port is listen as the the service provide
Reference:
Connecting remote SQL Server to PHP.
How to connect to a remote SQL Server.
If it can runs correctly with no error on local computer, then deploy you app to Azure application service.
Here are two examples about connect to the MSSQL:
1.connect to a specified MS SQL Server database:
$c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
2.connect to a MS SQL Server database on a specified port:
$c = new PDO("sqlsrv:Server=localhost,1521;Database=testdb", "UserName", "Password");
Hope this helps.
Related
I'm using Microsoft Azure SQL for my database and I'm trying to connect it to my project on my localhost using XAMPP. When I try to connect to the database using the connect string they provided:
try {
$conn = new PDO("sqlsrv:server = tcp:app.database.windows.net,1433; Database = mydatabase", "{myusername}", "{your_password_here}");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
I keep getting this error:
Error connecting to SQL Server.PDOException Object ( [message:protected] => could not find driver
The error is longer but I think it's related to the driver mentioned at the end. I'm using a mac so any driver I need to install would need to be compatible with it. Thank you for the help in advance.
Azure SQL Database is built on the Microsoft SQL Server engine. So you might consider using pdo-dblib to connect to SQL Server using the PDO on a Mac. You can check out a Stack Overflow question about it here or refer to this setup documentation.
After the driver having been installed, Azure SQL won’t actually accept connection from your local yet. And you might get the following error:
Client with IP address '167.xxx.xxx.xxx' is not allowed to access the
server.
That’s because there is a firewall in the way by default. To enable access, go to the Azure portal, click on All Resources, select your SQL service, click on Firewall in the SETTING menu.
Your client address is conveniently included in the list, so you can just click on Add client IP followed by Save.
Well, when you run your code now, it should connect.
I am currently working with php. I want to connect the PHP with sql server but it is not working on the production server.It worked perfectly on my local machine, as I installed the sql server driver in the extension folder of php and activated it in php-ini file.I confirmed that the driver has been loaded into the production driver as it was hosted in a windows server and not a linux server.I need assistance,because the same script with which I connected the my local machine to the cloud sql server is the same script i am using now to connect the php application to the same sql server that is now not working. Please, I need help. take a look at my connection script that works perfectly with my local machine.
$serverName = "remote sql server IP"; //a.b.c.d
$connectionInfo = array("Database"=>"sqlserverDBname", "UID"=>"sqlsvruserID", "PWD"=>"sqlsvrPassword");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn){
echo "Connection Established<br/>";
}else{
echo "Connection could not be established<br/>";
die(print_r(sqlsvr_errors(),true));
}
?>
Both the php application and the other application that uses sql server are on the same domain, while php-app and other-app are sub domain. Kindly help look at this. Thanks community for anticipated advice/solution
Please add Server Name(host) instead of remote sql server IP $serverName = "remote sql server IP";
This will solve your problem i think.
I have a Linux server running Wordpress 4.2 and I have 2 Azure DB, one is SQL Server and the other is CleanDB.
The Windows Server(s)
Windows 7 Pro/Windows Server 2012 R2 sp1
Visual Studio 2013
SQL Server 2012 sp1
PHP 5.4 WP 4.2
Linux Server
CentOS 6.0
WP 4.2
PHP 5.4.31
It runs on SQL server, visual studio, and my php script on my Windows server to the CleanDB and SQL Server run fine and when I run the connection string on the Linux server to the CleanDB.
It won't connect when I run the Linux server to the SQL server.
I only allowed the DB to allow 4 calls and it isn't pulling.
The IP ranges were added on the firewall.
I used the exact same string in all instances.
I found a few other related questions that some help:
SO References:
Cannot connect to azure db via SqlConnection
"Call to undefined function sqlsrv_connect()" when trying to connect to Azure DB from PHP
MSDN References:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-php-how-to-use/
https://azure.microsoft.com/en-us/documentation/articles/web-sites-hybrid-connection-connect-on-premises-sql-server/
it won't connect when I run the Linux server to the SQL server.
Do you mean you couldn’t connect to SQL Azure from a Linux server while using sqlsrv drive? Please note, SqlSrv is a Windows driver, on Linux we can use PDO_DBLIB drive and PDO_ODBC (not recommend), more detailed info could be found at: http://php.net/manual/en/ref.pdo-dblib.php.
Code snippet via using PDO_DBLIB for your reference:
<?php
$dbHost = 'YourName.database.windows.net';
$dbUser = 'DBUserName';
$dbPass = 'DB Password';
$dbName = 'DB Name';
try {
$pdo = new PDO("dblib:host=$dbHost:1433;dbname=$dbName", $dbUser, $dbPass);
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
Code snippet via using PDO_ODBC for your reference:
$connection = odbc_connect("Driver={SQL Server};Server=$dbHost; Database=$dbName;", $dbUser, $dbPass);
$qry = "SELECT * FROM Clienti";
$result = odbc_exec($connection,$qry);
// Get Data From Result
while ($data[] = odbc_fetch_array($result));
// Free Result
odbc_free_result($result);
// Close Connection
odbc_close($conn);
// Show data
print_r($data);
AS to DB settings, please do not forget to manage allowed IP addresses on Azure portal, i.e. add your client IP address in Allowed IP Addresses section on Azure portal. https://msdn.microsoft.com/en-us/library/azure/ee621782.aspx#Troubleshooting
Feel free to let me know if I have any misunderstood on your issue.
I am trying to connect to a SQL Server database from PHP but I'm having problems with the connection string. Here is a rundown of what I've tried and the effect it has had:
Running PHP 5.3.13, Apache 2.2.22, Windows 7, and SQL Server 2008 R2
I've installed SQL Server Native Client 11.0
I've dropped using MSSQL driver, using the SQLSRV driver instead.
Loaded these extensions in php.ini: extension=php_pdo_sqlsrv_53_ts.dll, extension=php_sqlsrv_53_ts.dll,
extension=php_pdo.dll.
phpinfo() shows these as active:
Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar, sqlsrv
PDO drivers mysql, odbc, sqlite, sqlsrv
sqlsrv support enabled
Connection code is as follows:
$serverName = "[servername]";
$connectionInfo = array( "Database"=>"[databasename]");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
The string is set up to use Windows authentication, which sqlsrv_errors() reports:
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[networkname]\[machinename]'
I have also tried this using my network id/pwd, resulting in
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[username]'
I do know that the database is up and functional, as I can connect and run queries without problem from the SQL Server client, and from sqlcmd. The SQL Client and the command line work when using my network/machine credentials. The command line client fails when I try with username/password. This would indicate that any PHP connection should use the Windows authentication to connect.
Someone in another thread suggested that changing permissions in a registry entry would work, but this did not help my issue. PHP 5.3 not recognizing Native Client to connect to MS SQL
I also had some success creating a system DSN. The connection test reports success:
Microsoft SQL Server Native Client Version 11.00.2100
Running connectivity tests...
Attempting connection
Connection established
Verifying option settings
INFO: A network alias was found for the DSN server. 'Protocol: DBMSSOCN; Address: [servername]' was used to establish the connection.
Disconnecting from server
TESTS COMPLETED SUCCESSFULLY!
I've searched extensively within stackoverflow.com, and the interwebs in general with no luck. Obviously the db connection is alive using other methods, but what is going wrong in my connection via PHP?
I got this solved by creating a new login using sql server authentication. I hope this helps somebody with similar issues.
SQLSRV uses the absence of a UID (uid==NULL || strlen(uid) ==0) to indicate that it should try set the "trusted connection" flag in the connection string, i.e. connect using the credentials implied by the execution context.
You can't pass a domain login and password (e.g. UID='MYDOMAIN\user') explicitly.
I had a weird compiler version issue with Microsoft's driver for PHP, and solved my connection problem with PHP's ODBC driver:
//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for ODBC and PHP ODBC.
I am having issues connecting to a mssql server that is located on a network machine. I need to connect remotely through another domain example (abc.com) not on the network to access data. This domain runs mysql if that makes any difference. I am trying to access the mssql server through this php script:
<?php
$server= 'ip address:port';
$user='user';
$password= 'pass';
$con = mssql_connect($server,$user,$password);
if (!$con)
{
die('Could not connect:' . mssql_get_last_message() );
}
else{ echo 'connected';
}
I run this script through the shell on abc.com and I get:
mssql_connect(): Unable to connect to server:
I have gone through several tutorials to enabling tcp/ip access through the sql server config manager as well as allowing the specific port through the firewall.
What are some other things I should try or steps I am missing here.
Also: the ip address I am using is the one I found in the sql server config manager-> protocols for SQLEXPRESS->tcp/ip_>ip addresses->IP2->ip adress it is the correct ip address? Where can I find it if not? I am not using the localhost 127.0.0.1
In short to successfully execute the cmd.
config:
(2 instances of SQL // SQL2005 + 2008 Express instance on the remote machine, which refuses to install Management Studio 2008.
1. Enable TCP/IP Protocol
2. Enable Named Piptes
3. started sqlcmd Utility with -s \SQLEXPRESS to get the right sever-instance
C:\Program Files\Microsoft SQL Server\100\Tools\Binn
4. Go to SMSS and local sql instance properties -> Connections -> check "Allow remote connections to this server" and run this script.
EXEC sys.sp_configure N'remote access', N'1'
GO
RECONFIGURE WITH OVERRIDE
GO
Here the link for the info from Msdn:
http://msdn.microsoft.com/en-us/library/ms162773.aspx
http://msdn.microsoft.com/en-us/library/ms162816.aspx
Thanx