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.
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'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.
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)
Following an OSX 10.9.3 upgrade a number of things didn't work. Apache, Mongo and PHP are all working independently now, however the mongo extension for php is not. I am hoping the stackoverflow community can help. Here's the basic problem:
$ sudo pecl install mongo
pecl/mongo is already installed and is the same as the released version 1.5.3
install failed
$ php --re mongo
Exception: Extension mongo does not exist
Pecl and PHP are having a disagreement on whether the mongo extension is there. mongo.so does physically exist here: /opt/local/lib/php/extensions/no-debug-non-zts-20090626/mongo.so where it was installed and here: /usr/lib/php/extensions/no-debug-non-zts-20100525 where I copied it because that is where php.ini points. It is executable in both places. It seems odd that it is getting installed in the wrong spot, so maybe there is a pecl config that needs to be flipped? Also pecl is not finding php.ini at the end of the install, but I am updating manually.
The best summary of the directions for getting back up after the 10.9 upgrade appears to be here: http://fighterpilotstress.blogspot.com/2013/10/installing-mongodb-driver-with-php-on.html and I have followed it faithfully. I have also installed the commandline tools as referenced here: Unable to install mongodb php driver on mac os 10.9.
Relavent php.ini lines:
include_path = ".:/usr/lib/php/pear"
extension=mongo.so
any help appreciated. Thanks, Brian
I hanged up for a while in the same situation on ubuntu, then I just tried
1) In your phpinfo() screen, make sure the Configuration file Path and Loaded Configuration File match the PHP file you are editing. If not, then find the correct php.ini and add the mongo.so extension.
2) In your phpinfo() screen, look at the extension_dir value and confirm that mongo.so exists in that directory. If not, find the mongo.so file and copy it to this directory.
3) Restart your web server.
4) If it's still not working, take a look at the web server & php logs for clues as to why the extension might not be loading.
from Error enabling MongoDB module
All was ok on my side, but still I kept getting,
$ php --re mongo
Exception: Extension mongo does not exist
then I checked with this test script and was able to connect to mongoDB.
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query
// retrieve all documents
$cursor = $collection->find();
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Quantity: ' . $obj['quantity'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
Therefore try the script and if you are able to connect without errors, forget about the extension :).