Connect to SQL Server 2008 from PHP - php

Using the PHP drivers from Microsoft, we cannot connect using either Windows Authentication or SQL Server authentication. We use the following PHP code:
$serverName = "(A\B)";
$connectionInfo = array( "UID"=>"u1",
"PWD"=>"p1",
"Database"=>"db1");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

In a nutshell the php sql server stuff is terrible.
The only reliable solution (and I am forced to use SQL Server 2008 here for a very complex project) is using PHP ADODB.

Related

Setup MS SQL Remote Connection & Connect From Linux Hosting With PHP

Oke soo as the title said,
I have SQL Server 2005 installed on my windows 7 at home, and I have a website which is using linux (Ubuntu 14.04, can be upgraded if necessary),and the questions is
How to setup remote connection for SQL Server
What kind of driver or addons that I need to use for PHP on linux? SQLSRV ? is the driver safe to use?
Example for the driver that used to connect to SQL Server
And I see that people use ip to connect remotely, how do I get the ip for connecting to my own pc?
To setup remote connections follow the instructions here: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-remote-access-server-configuration-option
Yes the driver is safe. Getting started instructions here: https://www.microsoft.com/en-us/sql-server/developer-get-started/php/windows/
Example code
<?php
$serverName = "localhost";
$connectionOptions = array(
"Database" => "SampleDB",
"Uid" => "sa",
"PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn)
echo "Connected!"
?>
You can get ip from ipconfig

PHP connectivity with MS SQL using odbc_connect

I am working on a PHP project where I have to create an API to fetch data from client's MS SQL server. I am doing it as-
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server_ip;Database=$databse;", $username, $password);
Its working on my localhost(might be because I have SQL Server install on my system, but not sure). But does not work on hosting server. It gives error-
[unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 10.0' : file not found
I have even tried -
$serverName = $server_ip;
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$conn does not return anything.
Is there any other alternative?
I have even got odbc driver install.
There is an alternative (extension) called php-sybase. I am using it successfully to connect to various (versions) MSSQL databases with different php versions, to mention just: 5.6 and 7.0.

Wordpress MS SQL Query with PHP

I have my wordpress page and I want to connect with an external MS SQL SERVER DB.
I am using a plugin that allows me to put PHP CODE in there. This is my code:
$serverName = "192.168.2.3";
$connectionInfo = array( "Database"=>"v3v", "UID"=>"sa", "PWD"=>"1234567");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
The problem is that sqlsrv_connect is not activated. I have been looking for information and it seems that I need some drivers for my php version(5.6). I got the .exe file that install .dll(the drivers) but I don't know where to put it. I mean, Where is my php configuration with wordpress?
With xampp it was located at PHP/ext.
If you have another solution to do this simply query I would appreciate it because I know that Wordpress likes to work with MySql but I have to connect with MS SQL SERVER.

PHP - Can Connect to SQL Server 2008R2 with sqlsrv_connect but not with PDO - why?

I've been trying to connect to a SQL Server 2008R2 database using PDO in PHP, using this syntax:
$conn = new PDO('sqlsrv:Server=xxxxxx;Database=xxxxx', 'xxxxx', 'xxxxx');
but each time, I try to run a query, I get "exception 'PDOException' with message 'SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. '"
But when I try using
$serverName = "xxxxx";
$connectionInfo = array( "UID"=>"xxxx",
"PWD"=>"xxxx",
"Database"=>"xxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
The query is successful.
My PHPinfo() shows that sqlsrv is enabled in PDO drivers and the fact that sqlsrv_connect is working would seem to suggest that it's not a firewall or port opening issue. Username and password are identical in both cases.
Can anyone help suggest where I should be looking to get the PDO working, as I'd prefer to do it this way for consistency?
AAAARGHH, it was a syntax error in my PDO statement. Changed everything to lower case and it worked. Sorry, StackOverflow!

Connecting to MSSQL from PHP securely with encryption?

I need to connect to a MSSQL database from PHP. However, as a server on a remote site is connected, I require the connection to be encrypted.
Is it possible to use encrypt the connection to the MSSQL server using only mssql extension for PHP or alternatively PDO?
There is 3 things that are important when implementing a secure (encrypted) connection to MSSQL:
The options Encrypt and TrustServerCertificate are often used together.
By default the SQL server installs a self-signed certificate that it will use to encrypt connections - the self signed certificate are however open to attacks. So it should be replaced with one from a certificate authority (CA).
After replacing your certificate, you then set Encrypt = true and TrustServerCertificate = false (TrustServerCertificate = true will also work, but your connection will then be vulnerable to attacks)
Code-example from article *1:
$serverName = "serverName";
$connectionInfo = array( "Database"=>"DbName",
"UID"=>"UserName",
"PWD"=>"Password",
"Encrypt"=>true,
"TrustServerCertificate"=>false);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
If you use PDO create an object and pass the relevant params.
For a more detailed explanation please see the following article:
*1 - http://blogs.msdn.com/b/brian_swan/archive/2011/03/08/sql-server-driver-for-php-connection-options-encrypt.aspx

Categories