I've been trying to establish a connection between a PHP script and a Microsoft Azure SQL database but keep running into this same issue. I've tried Xampp version 7.1.11 / PHP 7.1.11, 7.0.25 / PHP 7.0.25, and now I am using 5.6.32 / PHP 5.6.32.
I've installed the Microsoft Drivers for PHP for Microsoft SQL Server and am using the SQLSRV32.EXE for Version 5.6.32 which is added to the C:\xampp\php\ext folder.
When I edit the php.ini file to add extension=php_sqlsrv_56_nts.dll save and reboot apache in the php error log file I keep getting the error
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_sqlsrv_56_nts.dll' - The specified module could not be found. in Unknown on line 0
Each version of XAMPP that I've tried has done this. Can anyone lend an idea or a hand how I can get around this and complete my SQL connection using PHP to an Azure SQL database?
First, open a phpinfo() and search for the line Thread safety. If it says enabled, then you should use the Thread-Safe DLLs instead.
How to configure XAMPP to use with MSSQL?
So after further tinkering and research, I've finally solved the issue I've mentioned above. Unfortunately, the common google search provides this Microsoft Drivers for PHP for SQL Server which only contains drivers up to PHP Version 7.0.XX. For PHP Version 7.1.12 I've used the thread safe1 php_sqlsrv_71_ts_x64.dll found here Microsoft Drivers 4.3 for PHP for SQL Server. I'm not sure why it took me another three days to find this.
The connection to the Azure SQL Database now works, the steps are mentioned below.
1. Acquire the correct SQL driver from Microsoft
-If you are using PHP Version 7.0 or earlier the drivers can be found Here
-If you are using PHP Version 7.1 the drivers can be found Here
2. Install Microsoft ODBC Driver 13 for SQL Server
3. Install required .dll's to the php\ext folder. eg. C:\php\ext
4. Edit the php.ini file and ensure the extension_dir= points to the C:\php\ext folder that you placed the downloaded .dll's (drivers) in.
5. Add extension=required .dll name to the php.ini file approximately line 890. In my case this is extension=php_sqlsrv_71_ts_x64.dll
6. Save php.ini and restart Apache.
The PHP script I've used to test my connection is as follows: sqltest.php
$connectionInfo = array("UID" => "USERNAME", "pwd" => "PASSWORD", "Database" => "DBNAME", "LoginTimeout" => 30, "Encrypt" => 1, "TrustServerCertificate" => 0);
$serverName = "SERVERNAME";
$conn = sqlsrv_connect($serverName, $connectionInfo);
if (sqlsrv_errors($conn)) {
die('Failed to connect to Azure SQL: '.sqlsrv_errors());
} else {
echo "Connection to Microsoft Azure SQL Server has succeeded! <br /><br />";
}
$tsql = "SELECT * FROM [Users];";
$getResults = sqlsrv_query($conn, $tsql);
echo ("Reading data from table <br />" . PHP_EOL);
if ($getResults == FALSE)
echo(sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['Id'] . " " . $row['Name'] . " " . $row['Username'] . " " . $row['Password'] . "<br /> " . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
Further information is provided by Microsoft Here
Thanks again for all of the helpful input from everyone above.
Related
I am trying to connect my SQL server with PHP using Xampp. I have already uploaded dll files in the ext folder but I am unable to connect it.
My PHP version is 7.2.6.
Uploaded dll files are - php_pdo_sqlsrv_72_ts.dll, php_sqlsrv_72_ts.dll.
I have written this code to connect my SQL database with PHP-
<?php
$serverName = "INDO-SERV\SQLEXPRESS,1443";
$uid = "sa";
$pwd = "XXXXXX";
$databaseName = "web";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>$databaseName);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);
?>
I am getting this error when I had tried this-
Fatal error: Uncaught Error: Call to undefined function
sqlsrv_connect() in C:\xampp\htdocs\biometric\db.php:7 Stack trace: #0
{main} thrown in C:\xampp\htdocs\biometric\db.php on line 7.
Anyone has an idea where I am doing wrong or how to connect with the database.
Installation of PHP Driver for SQL Server (sqlsrv and/or pdo_sqlsrv PHP extensions) can be done following the next steps:
Based on Microsoft PHP Drivers for SQL Server Support Matrix download appropriate version of this driver. In your case - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
Download and install an appropriate ODBC driver - see System Requirements for the Microsoft Drivers for PHP for SQL Server
Load PHP Driver for SQL Server as PHP extension.
Restart Apache
Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).
my XAMPP version is 7.0.13
1- Download and Install "SQLSRV40.EXE" on this path:
D:\xampp\php\ext
2- Download and Install "msodbcsql.msi"
3- Edit file ":\xampp\php\php.ini" and add this extensions :
extension=php_sqlsrv_7_ts_x86.dll
extension=php_pdo_sqlsrv_7_ts_x86.dll
extension=php7ts.dll
4- restart xampp
Note: "SQLSRV40.EXE" Contain this extensions:
php_sqlsrv_7_ts_x86.dll , php_pdo_sqlsrv_7_ts_x86.dll , php7ts.dll
Download driver from:
https://download.microsoft.com/download/f/4/d/f4d95d48-74ae-4d72-a602-02145a5f29c8/SQLSRV510.ZIP
Unzip the files
Copy the dll files in C:\xampp\php\ext\
Open with your favourite editor the file php.ini located in C:\xampp\php\
Insert the extensions:
extension=pdo_sqlsrv_74_ts_x64
extension=sqlsrv_74_ts_x64
Restart Apache and PHP
For the new comers;
You can setup driver and integrate it as in this video explains so in a nutshell;
You should find drivers for php - sql server integration depending to your environment (versions) at links and download the driver that suits for your environment.
You should move the driver file (.dll for windows case) to php/ext folder.
You need to change php.ini as entering a new extension (for example extension=php_sqlsrv_7_ts.dll) by giving your exact file name you have moved to php/ext.
Restart your local server and you should see this extension in phpinfo(), if you can see it's there, you can connect to your db with your credentials and it's done.
Credits goes to creator of the video (applause) :)
My server is a Windows 2008 server. PHP Version 7.2.7 is installed and running. Sql Server 11 (64 bit) is installed and is working (there is a couple asp.net apps running and already using that database)
I downloaded the PHP Sql Server Drivers from Microsofts website and placed the .dll files in the PHP ext directory.
In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64
In my .php file I am using to test my db connection I have:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
I am getting:
PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7 (Line 7 is the $SqlServerCon line).
What did I do wrong? and What do I need to do to get a connection to Sql Server?
I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.
For future reference for anyone else with this or a similar issue...
It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.
In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
More info can be found at https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.
PDO connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
sqlsrv_connect connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
As commentators said - adding .dll at the end of extension= config line should be good start with your issue.
I can also see that you're trying to load NTS version of extension (NTS stands for non thread safe). Are you sure that you're going to load right version of extension? Please check if you're running PHP as NTS runtime or not - you can check it with phpinfo();.
I have been thrown into the world of PHP, and I'm having a bit of an issue connecting to my SQL server. I'm using the following code to try to connect. When it hits the SQLsrv_connect command, it just seems to stop processing. No error, it just stops loading.
function testConnection()
{
$returnable = "TEST";
echo 'Current PHP version: ' . phpversion();
$connectionInfo = array("Database"=>"MyDatabase", "UID"=>"MyLogin", "PWD"=>"MyPassword");
$conn = sqlsrv_connect("MyServer",$connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
}
return $returnable;
}
Any idea on what I might be missing? I tried some very old syntax for version 5, and I got the same issue. I am trying to connect to sql server 2008.
Thanks
First, check if you have installed the sql_srv extension for PHP.
Probably this extension is not installed/loaded by you php.ini file.
For Windows
Download proper driver from SQL Driver for MSSQL extract and copy into you php installation directory. Then edit php.ini file and add in extensions path your extension. (For 99% you should copy NTS sql_srv version) also don't forget add sql_srv_pdo extension.
For Ubuntu/Linux
You can try install sql_srv and pdo_sql_srv by pecl (if is installed)
pecl install sqlsrv pdo_sqlsrv.
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)
phpinfo
_ENV["ORACLE_HOME"] C:\oracle\instantclient_11_2\
_ENV["OS"] Windows_NT
_ENV["Path"] C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\oracle\instantclient_11_2;\;
oci8
OCI8 Support enabled
Version 1.2.5
Revision $Revision: 1.269.2.16.2.43 $
Active Persistent Connections 0
Active Connections 0
Temporary Lob support enabled
Collections support enabled
php code
<?php
$conn = OCILogon('mppd1','mppd1', "121.256.476.86:1521/mydatabase");
$query = 'select * from users';
$stid = OCIParse($conn, $query);
//OCIExecute($stid, OCI_DEFAULT);
while ($succ = OCIFetchInto($stid, $row)) {
foreach ($row as $item) {
echo $item." ";
}
echo "<br>\n";
}
OCILogoff($conn);
?>
i am getting this error
Severity: Warning
Message: ocilogon() [function.ocilogon]: OCIEnvNlsCreate() failed. There is something wrong with your system - please check that PATH includes the directory with Oracle Instant Client libraries
I solved it copying all the content of C:\instantclient_11_2 (please check what´s yours) inside system and system32 folders in Windows , then I delete the path of C:\instantclient_11_2 in the PATH enviroment variable.
I am using XAMPP and Windows 8 and it´s the first time I see this issue. I always configured properly oci 8 with xampp and windows in a few minutes. I hope this would help you.
You need to copy all content of the instant client to apache/bin
im using xampp and working for me.
copy all files of the instant client enter image description here to apache/bin
I was facing the same error on uwamp 3 in connecting to oracle 11gR2.
I deleted the oracle instantclient from path variable and copied all files from instantclient to uwamp\bin\apache\bin
and it worked.
My setup:
System: Windows 7
Instantclient: instantclient-basiclite-win32-11.1.0.7.0
Web Server: Uwamp3
https://forums.oracle.com/forums/message.jspa?messageID=1742926#1745145
There are several potential solutions on that page ranging from re-installing xampp to checking permissions to using native php oci_connect(). Have you tried any of these things?
Probably you should download InstantClient and replace contents of /instantclient folder of Oracle client with the .dll-s of InstantClient.