Call to undefined function sqlsrv_connect() - Troubleshooting - php

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);

Related

How do I setup sqlsvr in PHP 8.1 and above?

Currently I have PHP 8.1.14,and right now when I try to use sqlsrv_connect() it will only show me this error:
Uncaught Error: Call to undefined function sqlsrv_connect().
After some researching I realized that I need to download some extensions for my PHP in order to use it.
The two .dll I've downloaded are and placed in my C:\xampp\php\ext folder are:
php_pdo_sqlsrv_81_ts_x64.dll and php_sqlsrv_81_ts_x64.dll
Which I am not certain if I have downloaded the correct one.
I have also added these extensions in the php.ini file under the Dynamic Extension section.
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=shmop
extension=php_pdo_sqlsrv_81_ts_x64.dll
extension=php_sqlsrv_81_ts_x64.dll
Also, these are the line of codes that i used to call sqlsvr_connect() function
<?php
$serverName = " **server name**, **Port Number**"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"**Database**", "UID"=>"**user ID**", "PWD"=>"**Password**");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "<script>alert('Could not establish connection!');
";
die("window.history.go(-1);</script>");
}
?>
Would highly appreciate any help regarding this matter! Thank you in advance.

How to install msodbcsql in microsoft AZURE server (PaaS)

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));
}
?>

Cannot connect PHP to SQLServer 2012

I'm using xampp and I'm trying to connect my PHP project to SQL Server to make reports. But there's a conflict with sqlsrv_connect() function.
My version of PHP installed in XAMPP is 5.6.23 and my connection file contains this:
<?php
$serverName = "MyserverName\MyinstanceName"; //serverName\instanceName
$connectionInfo = array("Database"=>"Database_Example");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Conexión establecida.<br />";
}else{
echo "La conexión no se pudo establecer.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Also, I have downloaded the next files and I have extracted in "C:\xampp\php". PHP dll's
And I have added to "php.ini" file this code:
extension=php_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_ts_x64.dll
I´ve tried too with the exact version of the driver for my PHP and it doesn´t work.
Drivers Version
And this is the error that appears when I'm trying to connect:
Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\Inventory\conexion.proc.php
I have solved my own problem! I write the answer for someone with the same problem.
The version of the driver have to be the same of your PHP, not superior. You have to extract the dll files in "C:\xampp\php\ext" not in "C:\xampp\php".
After that another problem will appear:
ODBC Driver 11
You have to dowload ODBC Driver 11 on this link: https://www.microsoft.com/en-us/download/confirmation.aspx?id=36434
And then when the driver will be installed, your code with queries will work!

Connecting to microsoft sql server using php

I am trying to connect to a mssql server using php. The sql server is on a different machine in the network and I have XAMPP installed in my machine. I don't have microsoft sql installed in my server.
I have downloaded the sqlsrv drivers for PHP and then in my php.ini file added the extension extension=php_pdo_sqlsrv_55_ts.dll under windows extension.
Added the php_pdo_sqlsrv_55_ts.dll file inside the php\ext\ folder of my XAMPP installation.
After restarting apache phpinfo(); shows that the sqlsrv driver is enabled
PDO support enabled
PDO drivers mysql, sqlite, sqlsrv
This is my code
<?php
$serverName = "192.168.100.102, 1433";
$connectionInfo = array( "Database"=>"ATP", "UID"=>"raihan", "PWD"=>"temp123#");
$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 on executing I get this error
Fatal error: Call to undefined function sqlsrv_connect() in
D:\xampp\htdocs\database\index.php on line 4
Why this error? And how do I go about connecting to mssql server using php?
php\etc\ sounds like wrong folder, try put in php\ext\ folder
Note extension=php_pdo_sqlsrv_55_ts.dll is used by PDO class
Go to https://www.microsoft.com/en-us/download/details.aspx?id=20098
Select download by your PHP version:
Select SQLSRV30.EXE if use SQL Server 2005
Select SQLSRV31.EXE or SQLSRV31.EXE if use SQL Server 2008
After download, extract dlls
If you use php with ThreadSafe (apache2handler -- more probable) copy php_sqlsrv_55_ts.dll fot ext folder
For use sqlsrv uncomment (or put) this line in php.ini extension=php_sqlsrv_55_ts.dll (or extension=php_sqlsrv_55_nts.dll for no-thread-safe)

i get a Fatal error when i try to connect mssql using php in wampserver

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.

Categories