PHP connectivity with MS SQL using odbc_connect - php

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.

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

Access Azure SQL Database from LAMP localhost in PHP

I would like to connect to by Azure SQL database by using a PHP-Script. I execute the following script by using LAMP:
<?php
$serverName = "tcp:***.database.windows.net, 1433";
$connectionOptions = array("Database" => "TryMe",
"UID" => "***#serverID",
"PWD" => "***");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
?>
Of course I checked the firewall rules on Azure. I allowed my IP to access the server.
But I'm getting the error message:
GET http://localhost/Ionic/test.php in the web console.
What I'm doing wrong?
Thanks for your help!
The function sqlsrv_connect requires the Microsoft Drivers for PHP for SQL Server which is only enabled in Windows system. As you are working in LAMP environment, you can try to use ODBC extension and Microsoft's SQL Server ODBC Driver for Linux.

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!

Connect to SQL Server 2008 from 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.

Categories