Setup MS SQL Remote Connection & Connect From Linux Hosting With PHP - 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

Related

PDO Error “Adaptive Server is unavailable” when connecting to MS SQL Database

I'm trying to connect to a MSSQL database with odbc in php.
My issue is similar to this issue, I can connect with tsql, but not with php.
The answer to the problem not work because I think I don't have SELinux installed (I don't know what it is, but pacman not found this package (or with a similar name) on my computer)
I don't understand why it doesn't work, odbc is installed and detected by php
print_r(PDO::getAvailableDrivers());
Array ( [0] => odbc )
I'm connecting by doing that:
$dsn = 'odbc:Driver=FreeTDS;Server=127.0.0.1;Port:1433;Database=[my base name]';
$pdo = new PDO($dsn, $user, $password);
My base is not in local, I use a ssh tunel because the base in accessible only at my school, and we need an ssh tunnel. And it work, I can connect myself to the base with tsql.
When I was connecting PHP to our MS SQL server, I could never get a connection established using ODBC drivers.
Instead, I downloaded the official PHP > SQL SERVER drivers direct from Microsoft. You can find them here.
Once installed, you must configure your php.ini file to include the new drivers, restart your web server and then use the following to open a new connection:
$conn = new PDO("sqlsrv:Server=SERVER_IP,1433;Database=DATABASE_TO_OPEN;");

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.

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.

How to connect a Linux server with Wordpress 4.2 server to Azure SQL DB with PHP 5.4?

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.

Trouble Connecting to SQL Server using PHP from localhost

I'm having a lot of trouble trying to connect to a remote SQL Server from my local test environment. I can connect to the server using Toad for SQL Server, but I'm not sure why it's not working through PHP. Here's what I have:
$connectionInfo = array("Database" => "db",
"UID" => $user,
"PWD" => $pwd);
$connection = sqlsrv_connect("ip address", $connectionInfo);
if(!$connection) //error message
I've tried every possible combination I could think of - using the server/instance name, explicitly giving the port (1433), using Windows authentication by removing UID and PWD - all of which work fine with Toad. I don't know what else I can try, or what I'm missing.
Any assistance is appreciated.

Categories