'Using PHP to connect to a remote MSSQL database
My local environment is WAMP. For a few pages i have to access a remotely hosted MSSQL DB, run queries and get back results.
I have been supplied the following information(values masked):
- IP address of the remote MSSQL machine: 11.22.33.44
- Database Name: abcdefgh
- Username: db_username
- Password: db_password
I have never connected to a remotely hosted DB before. From the example at: http://php.net/manual/en/function.mssql-connect.php
I tried this in a test file, mssql-connect.php:
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '11.22.33.44\abcdefgh';
// Connect to MSSQL
$link = mssql_connect($server, 'db_username', 'db_password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I am somehow not able to connect. I tried all variations of the connection string:
$server = '11.22.33.44/abcdefgh';
$server = 'http://11.22.33.44\abcdefgh';
$server = 'https://11.22.33.44\abcdefgh';
$server = '11.22.33.44';
$server = '11.22.33.44:1431';
$server = '11.22.33.44,1431';
Every one of these(and variations of these) return 'Something went wrong while connecting to MSSQL'.
I have enabled mssql through php.ini and can see it phpinfo. Are there any additional strings that needs to be passed?
Solution 1:[1]
First, dont provide full access to your database here (unless your user and password are just placeholders). Second, You have to be sure that the host where your SQL is running, has enabled remote access. If your MSSQL server has not enabled remote access, no matter how you try to connect, you will always fail.
Solution 2:[2]
$server = '11.22.33.44';
// Connect to MSSQL
$link = mssql_connect($server, 'db_username', 'db_password');
above code should work only thing is that mssql is not allowed to configured remotely so its blocking access to your ip. you have to enable mssql to get accessed remotely
Solution 3:[3]
I had the same issue after some playing around I finally got it to connect using sqlsrv_query()
Not sure if it because of PHP version updates or something.
Do some research on this, it worked for me.
Solution 4:[4]
You can connect with that codes successfully.
$username ="WRITEUSER";
$password = "WRITEPASS";
$database = "WRITEDB";
$server = "WRITESERVERIP";
$connection = mssql_connect($server, $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($databaseb, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
} else{
// Success
}
Regards.
Solution 5:[5]
to connect your database remotely you must create the user account in your phpmyadmin and allow the privileges to that user account. while connecting to the database remotely use the username and password information which you specified while creating the user account.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | taxicala |
Solution 2 | |
Solution 3 | user315891 |
Solution 4 | SwiftDeveloper |
Solution 5 | Darshan Jadiye |