I trying to connect to an SQL Server in PHP. With XAMPP on my local machine, everything works well. But now I going to bring my application on the production server.
On this server there is installed the Microsoft IIS 6.1 and running the PHP version 7.0.7. I also installed the ODBC Driver from here. Next I decomment the following line in my php.ini file:
extension=php_sqlsrv_7_nts.dll
extension=php_pdo_sqlsrv_7_nts.dll
I got the files from the official microsoft site.
What's my problem?
Unfortunately, after I restarted the IIS. The PDO function throws the PDOException error with the following message:
could not find driver
For the connection I am using the following function which works pretty well on my local machine:
try {
$con = new PDO("sqlsrv:Server=" . SERVER . ";Database=" . DATABASE, USERNAME, PASSWORD);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "No connection: " . $e->getMessage();
exit;
}
What can I else do?
Here is detailed process if it's helpful for someone. PHP Version - 7.4
Download and extract the .dll files from this link - https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
Paste the files in C:\xampp\php\ext, your path could be different.
in php.ini add those two lines at bottom or in extension section.
extension=php_sqlsrv_74_ts_x64.dll
extension=php_pdo_sqlsrv_74_ts_x64.dll
Restart your Xampp server, I'll suggest restart your computer and everything will work without an issue then.
Check if SqlSRV enabled
Check using phpinfo() or http://localhost/dashboard/phpinfo.php at like this -
Hope, it will help someone.
After I found the error log on the Windows Server, I solved the error by myself.
I got this error in my log:
[21-Apr-2017 07:12:14 UTC] PHP Warning: PHP Startup: Unable to load dynamic library '...\ext\php_pdo_sqlsrv_7_nts.dll' - %1 is not a valid Win32 application. in Unknown on line 0
Then I downloaded again the driver and installed the x64-Driver. Finally It works without any problems.
please notice you must use the correct version of php_sqlsrv_xx_xts_xxx.dll and php_pdo_sqlsrv_xx_xts_xx.dll files.
for exmple if you use php version 7.4 and a 64 bit system and wamp you must download and use these files:
php_sqlsrv_74_ts_x64.dll
php_pdo_sqlsrv_74_ts_x64.dll
for download you can use this site:
https://go.microsoft.com/fwlink/?linkid=2152937
https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
It took some time for me to solve the 'No driver'-error. I went through some steps as mentioned here and found some other ones that helped me after new errors. For future references:
Download the latest drivers from Microsoft (as said by Julian Schmuckli).
Check if your XAMPP is 64 bits(!) with Phpinfo(). If you've got 32-bit, you need different drivers.
Add the drivers to your Php.ini file and save the dll's in your php/ext-folder (question of saber tabatabaee yazdi).
For the connection, use this code:
$dbh = new PDO ("sqlsrv:Server=$server;Database=$dbname",$username,$pw);
If you add a port, use:
$server = "192.168.1.15, 51022";
Where the IP (can be hostname to) is your server and 51022 your port.
Related
it's about more than one full day that i started sqlanywhere and i'm so interested to use it,
i worked before with sqlsrv for ms sql server and connection via php ,
but i have problem for connecting php to sqlanywhere
here is the warning message when i try to run test.php
Installation successful Using php-5.6.0_sqlanywhere.dll
Warning: sasql_connect(): SQLAnywhere: [-100] Database server not found in C:\xampp\htdocs\test.php on line 44
Connection failed
$conn = sasql_connect( "UID=DBA;PWD=sql" );
if( $conn ) {
echo "Connected successfully\n";
sasql_disconnect( $conn );
} else {
echo "Connection failed\n";
}
I'm using xampp server php 5.6 and sqlanywhere 17 developer edition.
already i downloaded the php extension and copied on ext and also added the line in php.ini (extension=php-5.6.0_sqlanywhere.dll)
demo database i ran it with cmd : dbeng17 "%SQLANYSAMP17%\demo.db".
via cmd i tried this also :
C:>dbping -d -c "uid=dba;
SQL Anywhere Server Ping U
Connected to SQL Anywhere
Ping database successful.
but when i try via php it says: Warning: sasql_connect(): SQLAnywhere: [-100] Database server not found in C:\xampp\htdocs\test.php on line 44
Connection failed
please someone tell me what is the problem ?
thank you very much.
Your PHP installation may have multiple files similar to php.ini. Make sure you are modifying the php.ini file listed in phpinfo().
- A web server such as Apache, IIS, or any web server that supports
PHP.
- PHP installed on the same computer as the web server.
- SQL Anywhere installed on the same computer as the web server.
- The client software can be used as well. SQL Anywhere PHP extension
Refrance : https://wiki.scn.sap.com/wiki/display/SQLANY/Getting+Started+with+SAP+SQL+Anywhere+and+PHP
It solved for me when I installed and used it on another system , I don't know I tried first on a system that MSSQL server was install and I couldn't run it and tried on another system that mssql server was not installed and solved :)
sorry for being late to answer and thank you very much for those who took time to help me.
I upgraded my PHP 5.6.30 (https://www.apachefriends.org/de/download.html) to PHP 7.0 (https://bitnami.com/stack/wamp/installer)
Everything worked fine so far and it reduces the loading time from my Page from 1,2 seconds to ~300 ms, when I use a MySQL-Database. But now I'm trying to connect to a MSSQL-Database with the following simple script, that worked fine with my old installation (PHP 5.6):
<?php
//Use the machine name and instance if multiple instances are used
$server = 'Server-Adress';
$user = '';
$pass = '';
//Define Port
$port='Port=1433';
$database = 'Databasename';
$connection_string = "DRIVER={SQL Server};SERVER=$server;$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
} else{
die("Connection could not be established.");
}
$sql = "SELECT * FROM st3_200 WHERE identifier = 1";
$result = odbc_exec($conn,$sql);
// Get Data From Result
while ($data[] = odbc_fetch_array($result));
// Free Result
odbc_free_result($result);
// Close Connection
odbc_close($conn);
// Show data
print_r($data);
?>
But now I got an error in my logs that says:
[Thu Dec 10 11:55:26.629956 2015] [:error] [pid 260:tid 968] [client
::1:63003] PHP Fatal error: Uncaught Error: Call to undefined
function odbc_connect() in
C:\Bitnami\wampstack-7.0.0-0\apache2\htdocs\test\query.php:11\nStack
trace:\n#0 {main}\n thrown in
C:\Bitnami\wampstack-7.0.0-0\apache2\htdocs\test\query.php on
line 11
First I thought, that my php.ini has a missing extension, so I enabled "extension=php_pdo_odbc.dll"
the difference from the php.ini in the 5.6 version is there is the extension:
"extension=php_mssql.dll" enabled. But I can't find them in the new PHP 7.0.ini
So my intension is there is no existing driver for odbc and PHP 7 yet?
I found some driver for Linux here:
https://aur.archlinux.org/packages/php7-odbc/
But I need something for my Windows environment.
Does anyone had the same issue and has already fixed it?
Thank und Greeting
Domi
Take a look in your php.ini, the string
extension=php_odbc.dll
seems to be missing in new installations, at least i had to add it manually in my new XAMPP installation (7.0.1)
and accidently just activated the pdo_odbc.dll
PHP7 has a few modules disabled by default that were previously enabled in PHP5.
It's an easy fix though since the extension should already exist in the \ext\ folder that came with PHP7. You just need to modify your php.ini file to include the line:
extension=php_odbc.dll
The line above is not already present and commented out; you actually need to add it!
PHP looks for the php.ini file in C:\Windows\ but it may also be located elsewhere on your machine. So check both C:\Windows\ and C:\php\ or where ever else you may have installed PHP.
After making the change you can check the results from the command line like this:
C:\php\php.exe -m
or (after restarting the web server / machine) from a .phtml file like this:
<? phpinfo(); ?>
That will output a list of enabled modules which should now include odbc; if not, then you may have modified the wrong php.ini file (keep looking) or forgot to restart the web server / machine.
Tips:
If you have a non-standard installation, you can use an absolute path like this:
extension=C:\php7x64\ext\php_odbc.dll
extension=php_mssql.dll (or extension=php_sqlsrv_56_nts.dll if you get it from Microsoft Drivers for PHP), is your problem: the Microsoft SQL driver for PHP 7 is not yet ready, the latest ETA is late January for the beta.
It looks like the cause of the delay is the intention to include SQL 2016 in that driver so you can migrate easier in the future.
UPDATE (2016/02/12):
As stated here (meet-bhagdev reply), there is an "early technical preview" of the PHP sqlsrv driver for Windows available on github.
Open your php.ini file and uncomment or add the following lines:
extension_dir = "C:\PHP\ext" ;<- your PHP path
extension=php_pdo_odbc.dll
extension=php_odbc.dll
Reset Internet Information Services:
On command prompt with admin rights type:
iisreset
This fixed the problem for me.
As an addition to Adrian B`s which mentions the official driver, you can also check https://github.com/thomsonreuters/msphpsql
This is an unofficial port. However there are limitations for the time being.
Supports only sqlsrv ODBC but not PDO
Doesn`t support ZTS, only NTS
Supports only x86
It supports a subset of ODBC functions , you can see the list on the page.
We need x86 driver from Microsoft.
http://www.microsoft.com/ja-jp/download/details.aspx?id=13255
*Sorry, 'ja-jp' is mine. Please select your country. There are 2 drivers, one is 32bit, but we need 64bit(x86) version.
Then we could set "ODBC data source (64bit)". It appears on the window.
Last, check your web. Maybe it works.
Mar. 29th. 2016 Naio
I guess you was right, you need download the SQL Server ODBC driver for your PHP client platform and OS.
Here is the link for the similar issue: Call to undefined function odbc_connect()
also you can try to install this connector for MySQL (if you use MySQL): Connector/ODBC
or if you use MSSQL: ODBC Driver 11 for SQL Server
I know this question is rather old. But I've come across the same issue recently ...
#Naio is right : there are indeed different versions of ODBC drivers, based on the architecture (32bits or 64bits). The driver that PHP uses depends on its own version.
In other words, if you are using a 32bits ODBC environement, make sure to use a 32bits version of PHP ...
My guess is that, by switching from PHP 5.6 to PHP 7.0 you also switched from a 32bits version to a 64bits version.
I have had a problem trying to establish a connection to a MSSQL database I created in Microsoft SQL Server Manager 2008 R2 using php 5.5.19 in XAMPP.
In the php.ino configuration file under extensions, there is commented out a ';extension=php_mssql.dll'. I removed the comment and ran apache in the xampp control panel.
Naturally an error occurred stating ''PHP Startup: Unable to load dynamic library '..\ext\php_mssql.dll cannot be found'. I looked in the ext directory and sure enough it wasn't there. I added the mssql.dll file from
www.dll-files.com/dllindex/dll-files.shtml?php_mssql
,but then I got an Entry Point not found error stating 'The procedure entry point php_body_write could not be located in the dll php5ts.dll'.
After further research, I discovered that mssql_connect was not working since PHP version 5.3: http://php.net/manual/en/intro.mssql.php
I instead tried to use the 'sqlsrv_connect'' function, but I had to install the new drivers from https://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
After reading the readme, I saw that I needed the libraries php_sqlsrv_55_ts.dll and php_pdo_sqlsrv_55_ts.dll to work with xampp's php5ts.dll in the php folder.
I added the libraries in the php\ext folder and added corresponding extensions to the new dll's and restarted Apache.
I then got an system error 'The program can't start because MCVCP110.dll is missing from your computer. Try reinstalling the program to fix this problem.' Obviously reinstalling isn't an option as I will lose my new dll's so I verified that MCVCP110.dll is in the php folder, which it was.
Please help, I really need to find a way to load the libraries with the 'sqlsrv_connect' function properly so I can connect to a MSSQL database.
I know there are some threads with this topic, but I'm a little desperate now so I think I should do a new one. I´m trying to connect sql server 2008 with php, I'm using WAMP server and php 5.4.3. I have read a lot about the configuration of the php.ini file and I did what I needed to do. But still I can´t get my connection working!
This is a little summary of what I´ve done
Install the Microsoft® SQL Server® 2012 Native Client 64 bits in my case.
Download and move to php/ext folder the drivers SQLSRV30.EXE
Using your PHP folder, run command: "C:\PHP\php.exe" -i | more one I did that i got the next info: HP Extension Build => API(big number here) TS , VC9
with this information I found i need this new lines in my php.ini file
For SQLSRV30.EXE, PHP 5.4, TS, VC9 add lines:
[PHP_SQLSRV]
extension=php_sqlsrv_54_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_ts.dll
Restart my server!!
Ok so I have a page whit this code:
<html>
<head>
<title>Resultado</title>
</head>
<body>
<?php
$server='ANDRES-PC';
$connectinfo=array("Database"=>"ejemplo");
//connect to DB
$db=sqlsrv_connect($server,$connectinfo);
if($db)
{
echo "Connection established.<br/>";
}
else
{
echo "Connection couldn't be established.<br/>";
die(print_r( sqlsrv_errors(), true));
}
?>
</body>
</html>
But when I try to see the page I got nothing!!! Not a single word. What do you thing is my problem. I used this page http://robsphp.blogspot.co.uk/2012/09/how-to-install-microsofts-sql-server.html to set up the connection!! please help
You've done everything correctly, so I'm guessing you have installed the 64 bit version of WAMP. Please see this answer:
http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/6dc1dc1a-5d74-415b-9225-ce11f481f301
Somewhere high up on my instructions page it says you must use 32 bit PHP:
"Microsoft's PHP driver for SQL Server only runs with PHP 32-bit builds"
Edit: never mind! I noticed your recent comment on my blog page: http://robsphp.blogspot.co.uk/2012/09/how-to-install-microsofts-sql-server.html
I'll leave this answer as it's a common reason for the sqlsrv driver not loading with WAMP.
Sqlsrv driver works only with 32-bit version of wamp. Install 32-bit version of wamp or connect to MSSQL using PDO connection with odbc driver
new PDO("odbc:dsn");
I have never connect PHP to MS sql server so getting confusion and trouble for configuring wamp to connect with sql server by following online tutorial. I have install wamp that consist of Apache Version 2.2.21 and PHP Version 5.3.8 and MS Sql server 2008 on same machine.
I downloaded the Microsoft Drivers for PHP for SQL Server(SQLSRV20.EXE). and extracted the file to D:\wamp\bin\php\php5.3.8\ext. Then I opened the php.ini file from wamp icon tray and provide the extension path as extension=php_sqlsrv_53_ts_vc9.dll and extension=php_sqlsrv_53_nts_vc9.dll.
I have put php file name testsqlserver.php in www root folder of wamp which code is shown below:
<?php
$server = 'mypc/SQLEXPRESS';
$link = mssql_connect($server,'sa','password');
if(!$link)
{
die('something went wrong');
}
?>
When I called testsqlserver.php through browser as localhost:8080\testsqlserver.php. It shows the error:
Fatal error: Call to undefined function mssql_connect() in D:\wamp\www\connectsqlserver.php
am I doing wrong way? or is there anything more to do for this.I have gone through different online search but unable to get the exact solution for this. Would someone help me, it would be great appreciation
You have to edit your php.ini file located within the WAMP directory. There should be a few lines in there for loading the MS SQL extension but they are commented out; all you need to do is uncomment those lines and restart Apache.
I'm using XAMPP and the lines within my php.ini that I have to uncomment are:
;extension=php_mssql.dll
;extension=php_pdo_mssql.dll
Microsoft Drivers for PHP for SQL Server which you have downloaded , support sqlsrv_connect() to establish connection with sql server database
You should use sqlsrv_connect() function to connect with sql server , mssql_connect is depreciated
you can get full list of function and their description form following link
http://msdn.microsoft.com/en-us/library/cc296152%28SQL.90%29.aspx
I do not currently use wamp, but I think you should enable PHP extensions for this. Click on wamp icon in status bar and enable php_mssql and php_pdo_mssql extensions, then restart apache service.