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
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
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!
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 open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. I've opened up the appropriate port on the firewall, but I am getting the vaguest error
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx
with:
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
Is there some way to get a more specific error message? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?
Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment...
As you can see, I use PDO rather than the mssql_* functions. On Ubuntu I needed to install the php5-sybase package to get the dblib driver.
PHP:
<?php
try{
$con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
exit;
}
?>
/etc/odbc.ini
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description = MSSQL Server
Driver = freetds
Database = MyDB
ServerName = mssqldb
TDS_Version = 8.0
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf
[global]
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[mssqldb]
host = mssqldb
port = 1433
tds version = 8.0
I ran into the same problem today. This works for me:
Instead of this
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
try this:
$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file
In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity
# Define a connection to the MSSQL server.
[mssqldbAlias]
host = mssqldb.mydomain.com
port = 1433
tds version = 8.0
The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file.
So your call becomes
$myServer = "mssqldbAlias";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
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);