I trying to connect to a MSSQL Server with PHP. For that I am using the following script:
function mssqli_connect($server,$username,$password,$database){
$connectionInfo = array("Database"=>$database, "UID"=>$username, "PWD"=>$password);
return sqlsrv_connect($server,$connectionInfo);
}
I am running PHP 7.0.7 on a Windows Server with the IIS 7.5. Unfortunately it doesn't work and I have no idea why.
What I've already tried
I dumped the function, but it only returns bool(false), which is not very helpful. I also tried to ping the server via cmd. This has succeeded. The credentials are also correct.
The extension php_sqlsrv_7_nts.dll is also requested in the php.ini, so it doesn't give me an error, that the function wasn't found.
Do I miss something, which is required?
I searched again. Now what I found is an other driver software, which is a msi executable file, which installs the odbc driver on the machine.
https://www.microsoft.com/de-ch/download/details.aspx?id=36434
I installed that software and now it works without any problems.
Thanks for all your comments.
Related
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.
I run a IIS Webserver with php 7.1.7 and the propriate SQL Anywhere extension.
Anywhere Client is installed in 32bit and 64bit version.
When connecting to database with
$conn = sasql_connect("UID=***;PWD=***;ENG=MyEng;LINKS=tcpip{host=192.168.2.204;port=2638}");
I receive as error:
The SQLAnywhere client libraries could not be loaded. Please ensure
that dbcapi.dll can be found in your PATH environment variable.
But all seems to be fine:
dbcapi.dll is in c:\Program Files\SQL Anywhere16\BIN32 and ...\BIN64
both paths are included in PATH variable and also shown in phpinfo
SQL Anywhere is shown in phpinfo output as well
What else can I check?
Regards
Florian
actually I don't know really why, as all settings, variables, taskes are same, but after a server reboot, now message is gone and it works.
Florian
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 have got FreeTDS, unixODBC and ODBC enabled on PHP. It all seems to work great. I could connect via iSQL command line.
For some reason PHP is not able to work when accessed via HTTP.
For example.
via command line if I run php index.php (where index.php connects via ODBC) it works well.
but the same code when accessed via HTTP, returns
Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect
Could this be a case of some permission
Thanks in advance
Here is the connection code
$connect = odbc_connect("DSNNAME", "USERNAME", "PASSWORD") or die('e');
I have changed the credentials. But the code works fine when I run it via PHP command through SSH via Terminal. But when I run it through HTTP it does not.
Also on SSH I am NOT running it via root user. I am using ec2 AWS - and ran SSH via ec2-user (which is the default)
I feel 'apache' user does not have permission to something which I am unaware of.
You can add the DSN from the PHP source code like this
$dsn = "Driver=FreeTDS;Server=192.168.1.17;Port=1433;Database=mydb;TDS_Version=7.2;";
$Connection = odbc_connect($dsn,$user,$pass);
2021 - BACK TO THE FUTURE:
I know that this post is old, but my reply can point a desperate soul in the right direction:
You are using odbc and the command "isql" works just fine
You are using FreeTDS and the command "tsql" works just fine
Trouble is running the php scripts that have calls to odbc via FreeTDS -> keeps giving errors
Kindly read:
https://www.linuxquestions.org/questions/blog/tix-592494/freettds-libiodbc-iodbc-php-7-4-21-on-slackware-current-5-13-5-38621/
I have been trying to connect to a ibm db2 database but it seems impposible. The as400 is in a different server than the one running php.
Everytime I do a db2_connect I get the following error:
Fatal error: Call to undefined function db2_connect()
How can I make this function work?
Tip: I've already tried with odbc and it was a lost of time, but I'm open to suggestions on that path as well.
[EDIT]
I finally changed to java... it was impossible with php...
Check your php.ini file and make sure it has the DB2 extension enabled. http://www.php.net/manual/en/install.pecl.php
Try this link:
http://www.theregister.co.uk/2006/08/09/db2_udb_part2/
Deals with:
Installing the PHP DB2 extension,
Creating a connection,
Obtaining a result set
Also gives as an alternative the PDO option.
But as for DB2 on the AS400, am not sure if something still needs to be installed on the
AS400 for this to work?
Anyway, have used the ODBC Client Access with no problems at all. What difficulty did you run into?