I'm trying to configure my QNAP TS-231P Web server to connect to a MS SQL database installed on a Windows server (MSSQL Express 2014).
In QNAP Control Panel -> Server Web, I added in the php.ini file this line
extension = mssql.so
and now running phpinfo() I see the mssql section (Library version is FreeTDS).
My web application, built on CodeIgniter 3.1.9, is unable to connect to MSSQL (it works on XAMPP ver 3.2.2 installed on Windows), I also tried this simple php code:
<?php
$connection = mssql_connect('10.10.10.100\SQLEXPRESS', 'sa', 'mypassword');
if (!$connection) {
die('Unable to connect!');
}
if (!mssql_select_db('MY_DATABASE', $connection)) {
die('Unable to select database!');
}
$result = mssql_query('SELECT * FROM MY_TABLE');
while ($row = mssql_fetch_array($result)) {
var_dump($row);
}
mssql_free_result($result);
?>
but the connection fails.
The question is what can I do for getting a successful connection ?
Based on information from php.net:
mssql_connect
Warning
This function was REMOVED in PHP 7.0.0.
Rather than it, you should use sqlsrv_connect:
To be able to connect to MS SQL Server, that server have to accept remote connections. See: Configure the remote access Server Configuration Option
When you use IP, you have to provide a port number on which MS SQL Server is listening remote connections.
<?php
$serverName = "serverName\\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
More about connection to MS SQL Server in this thread: How to use PHP to connect to sql server
Good luck!
Related
I want to connect to SQL Server at another pc with php, when i try to connect it says
Connection error: could not find driver
What I have tried is add extension in php.ini.
;extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
my PHP version is: 7.2.3
SQL SERVER: SQL Server 2014
OS PC: Windows Server 2012 R2 (another pc)
here's my code:
<?php
$host = "192.168.3.126";
$db_name = "dbo";
$username = "david";
$password = "david";
try {
//$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$db = new PDO("sqlsrv:Server={$host};Database={$db_name}", $username, $password);
}
catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage();}
?>
NB: I have download Microsoft Drivers 5.6 for PHP for SQL Server at here, I have download install the ODBC Driver 17 at my own pc
I have tried this reference but it doesn't work PHP Sql Server PDOException:could not find driver
My PC has Installed MS SQL Server 2008 R2 with installed Database, Then I installed XAMPP and made another port for Apache to make it localhost:8080 and listen to 4433. Then Installed OBDC, Tried to connect using
try {
$host = 'LOCAL';
$user = '****';
$pass = '****';
$dsn = "sqlsrv:Server=$host;Database=$db;";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$pdo = new PDO($dsn, $user, $pass, $opt);
} catch (PDOException $e) {
echo "No connection: " . $e->getMessage();
exit;
}
But I keep getting
No connection: SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
Used extension=php_pdo_sqlsrv_71_ts_x86.dll
I've shutdown Windows Firewall, Enabled Pipe Connection in MS SQL 2008 Configuration and Internet Option NetBIOS Enabeled
$host shouldn't be "LOCAL" but "localhost".
If you are running SQL server on a custom port on your local machine, you can specify the port by appending it with a comma to the host; applied to your code snippet:
$host = "localhost";
// or, with a custom port
$host = "localhost,12345"
Source: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php#refsect1-ref.pdo-sqlsrv.connection-examples
if you are using a local instance, the server string needs a double backslash:
<?php
$conn = new PDO('sqlsrv:Server=localhost\\SQLEXPRESS;Database=MyDatabase', 'MyUsername', 'MyPassword');
?>
See original provided answer by Daniel Klein:
https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php#121301
I'm trying to connect to a MSSQL database through PHP, but the connection doesn't seem to be ever made or it times out, not sure. When I load the file via localhost, I get the check1 debug statement but not check2. What am I missing? I can connect and navigate the database via MS Server Management Studio.
My environment
PHP Version 5.2.6
Window 7 64-bit
In my php.ini file, I added the following and verified those drivers are in the proper extension_dir.
extension=php_pdo_sqlsrv_53_ts.dll
extension=php_sqlsrv_53_ts.dll
Code below:
<?php
print_r("check1");
$serverName = "servername\instance";
$connectionInfo = array( "Database"=>"dbname", "UID"=>"user.name", "PWD"=>"abc$1s");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
print_r("check2");
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
edit:
Tried again after installing version 3 PHP SQL drivers and with PHP version 5.4.32 with no luck.
Set up:
Wamp Server
PHP 5.5.12
Apache 2.4.9
MS SQL Server 2012
Already completed:
Installed (unofficial) php_sqlsrv_55_ts and php_pdo_sqlsrv_55_ts AND confirmed they are working via phpinfo() sqlsrv_link
I have made sure that TCP/IP is enabled in SQL Configuration Manager
I have tested credentials in SQL Management Studio AND via ODBC in Administrrative Tools
IIS is disabled
Additionally, I did have trouble with SQL Reporting Service taking port 80. This was giving me a problem with Apache, so I directed SQL Reporting Service to use port 8081.
<?php
$server = "computer_name\MSSQLSERVER";
$user = "sa";
$pass = "password";
$db = "pcm";
$connInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);
$conn = sqlsrv_connect($server, $connInfo) or die( print_r( sqlsrv_errors(), true));
?>
If you are using the PDO extension, you will have to use a PDO connection instead of the sqlsrv_connect() function. Microsoft doesn't support the UID or PWD keys that you are providing when trying to connect via the PDO extension. Try this instead:
$conn = new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
http://php.net/manual/en/ref.pdo-sqlsrv.connection.php
I just set up my freetds.conf file with a new virtual account to connect to a SQL Server database but I am not sure when and where I tell my PHP script which database to connect to below are my settings
odbc.ini
[McDo]
ServerName = server1
Driver = FreeTDS
Description = MyServer
Trace = Yes
freetds.conf
[server1]
host = 66.111.xxx.xxx
port = 1433
tds version = 7.0
And here is my PHP connect script. Now sure where I tell the script to connect to which database.
putenv('ODBCINI=/etc/odbc.ini');
$connect = odbc_connect("server1", "username", "password");
//$query = "SELECT name_ID FROM ext_name";
if(!$connect){
echo "not connected";
}else{
echo "connected";
}
odbc_close($connect);
The php manual shows:
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);