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.
Related
I make sure that I can run website with WampServer,and also can connect the Sql Server.However,I run the bat file by the operating system scheduler,that return error:
OK,Can Run PHP
Fatal error: Uncaught Error:Call to undefined function sqlsrv_connect() in C:\wamp64\www\shell.php
Stack trace:
#0{main}
thrown in C:\wamp64\www\shell.php
My configuration : WampServer 3.0.6 64bits (PHP 7.0.10, Apache 2.4.23)
There are my steps:
First: I wrote a bat file in \wamp64\bin\php\php7.0.10
C:\wamp64\bin\php\php7.0.10\php.exe -q C:\test.php
PAUSE
Second: The function sql_connect is basic on php
echo "OK,Can Run PHP";
$serverName = "------";
$uid = "------";
$pwd = "------";
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"------","characterset"=>"utf-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false){
echo "Error<br>";
die( print_r( sqlsrv_errors(), true));
}
else{
echo "OK<br>";
}
Third: Put the bat in the operating system scheduler(Win7 64bit)
The cmd also can work by php -v and php -f.I think that cmd can work PHP with bat on the operating system scheduler because it answer "OK,Can Run PHP".
And then,the problem maybe is in "sqlsrv_connect()".
However,I really don't know that why the function "sqlsrv_connect()" can work in localhost website but can't work cmd without website. Please help me!!
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'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!
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.