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.
Related
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!
System Information
CMS: Wordpress
Web Server: XAMPP
PHP Version: 5.5.30
MS Management Studio 17
Goal
Establish MSSQL Database connection using PHP
What has been done
Downloaded SQLSRV Drivers
Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory C:\xampp\php\ext
Added the following lines to the dynamic extensions part in the php.ini file: extension=php_pdo_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_nts.dll
Restarted Web Server
Confirmed sqlsrv is listed in phpinfo()
Code
$serverName = "technology-pc\sqlexpress";
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$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));
}
Error
Call to undefined function sqlsrv_connect()
You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll.
Add to the php.ini:
extension=php_sqlsrv_55_ts.dll
or
extension=php_sqlsrv_55_nts.dll
Also, you really should be using either the Thread-Safe (_ts.dll) or Non-Thread-Safe (_nts.dll) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
You probably edited the wrong php.ini file. Check the right php.ini file with php info.
You can use this script:
<?php echo phpinfo(); ?>
Or if you have CLI access type php -i to get the info listed.
Check for the right path of your php.ini file.
Try below code to connect mssql database
$server = 'dburl.com\MSSQLSERVER, 1433';
$username = 'uname';
$password = 'password';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password,"ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect( $server, $connectionInfo);
Okay, so basically I have now deployed an Web App in Azure, but I cannot seem to make it to connect to a separate SQL SERVER. I have provided all the desired App settings for the connection but it seems no good.
Now I search for possible causes:
PHP ext dlls are missing for msodbcsql which are
php_pdo_sqlsrv_56_ts.dll
and
php_sqlsrv_56_ts.dll
I already added this dll's and referenced them accordingly base on this document Configure via ini settings
Now the uncertainty that msodbcsql is installed in Azure.
Now this is what I am having problem with.
So in my case in number 2 I tried copying the installer to Azure D:/home/
and run this in the command line
msiexec /quiet /passive /qn /i msodbcsql.msi IACCEPTMSODBCSQLLICENSETERMS=YES ADDLOCAL=ALL
which is suppose to install the .msi installer but i always get an
ACCESS IS DENIED
error.
is there another way around??
Basically Azure has already set up extension php_sqlsrv.dll & php_pdo_sqlsrv.dll for us, so we ourselves do not need to install any driver to connect SQL SERVER.
You could check the php.ini file which can be found at D:\local\Config\PHP-<version>\php.ini with Kudu console to confirm that.
Also, you can check that with phpinfo() function in your application.
And I have tested it with the following lines of code and got it worked.
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$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));
}
?>
I have configured wampserver 2.5
Apache/2.4.9 (Win64) PHP/5.5.12
I want to connect to SQL Server with PHP.
i have read wampserver doesnt support by default, so i downloaded the dlls, php_pdo_sqlsrv_55_ts.dll & php_sqlsrv_55_ts.dll and copied the same to ext folder of php(\bin\php\php5.5.12\ext) and added the extensions in php.ini file.
After that i have written a simple php file to check connect,
$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));
}
But i am getting error "Call to undefined function sqlsrv_connect()"
can i know why this error is still coming. How i can resolve the same.???
I'm using Wampserver (32bit & php 5.4I)2.4 *in windows 64bit*, will i install the 32bit becuse i need a connection to the MSSQL which required that as i know so far.
i downloaded the Microsoft Visual C++
and the php is a thread safe
so i added the two .dll files (php_pdo_sqlsrv_54_ts , php_sqlsrv_54_ts*) to the C:\wamp\bin\php\php5.4.16\ext*, then added them in the php.ini ( extension=php_pdo_sqlsrv_54_ts and extension=php_sqlsrv_54_ts), then restarted the wampserver
finally i gett an error says "Fatal error: Call to undefined function sqlsrv_connect()"
the code that i use is :
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"DB NAME");
$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));
}
?>
could anyone tell me what did i do wrong to cause this error?
Use phpinfo() to see if the extension was loaded correctly.
If yes, see the error log for any issues encountered when starting apache (W*A*MP).
If yes, then the extension binary is wrong or you forgot to enter the file extension (.dll on Win, .so on Linux) in the php.ini.