I am trying to develop a web app that can connect to as many different databases as possible on PHP. PDO (http://www.php.net/manual/en/book.pdo.php) seems to be the right interface for it but I am having trouble installing all the extentions needed for all the different PDO database drivers that I need.
Please note that I use xampp on a windows 7 machine. PHP Version 5.3.8. PDO drivers enabled mysql, odbc, sqlite, sqlite2, sqlsrv.
I have successfully connected with the following:
MySQL using PDO_MYSQL [MySQL (PDO) ] (extension seemed to be installed on xampp by default)
Microsoft SQL Server using PDO_SQLSRV [MS SQL Server (PDO)] (followed the instractions on http://craigballinger.com/blog/2011/08/usin-php-5-3-with-mssql-pdo-on-windows/)
I had no luck installing or connecting with:
(SOLVED SEE BELOW UPDATES) Sybase (I tried to use and install PDO_DBLIB [MS SQL Server (PDO)]but with no luck)
(SOLVED SEE BELOW UPDATES)Oracle (I tried to enable the extension=php_pdo_oci.dll in php.ini with the dll that was installed with xampp after restarting Apache the server failed to start. Was trying to use PDO_OCI [Oracle (PDO)])
I know I can work around those 2 with using the database specific drivers but I would really love to use PDO for everything that I need.
Does anyone know how to install and enable PDO_DBLIB and PDO_OCI drivers or a windows machine, or any other way of connecting with Sybase and Oracle databases using PDO?
UPDATE
Just succesfully connected with oracle with PDO_OCI. What you need to do is the following:
Download and install the proper Oracle Instant Client on your windows machine for
example instantclient_12_1 and add its path to PATH in SYSTEM
Environmental Variables. Note Oracle supports only 2 versions down so select
your client version properly. Do that and then restart your Apache. Note that the connection string is very different from here is a sample of what I used:
$tns = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$myServer.")(PORT = 1521)))(CONNECT_DATA=(SID=".$myDB.")))";
$connStr = "oci:dbname=".$tns;
$conn = new PDO($connStr,$myUser,$myPass);
UPDATE
Just connected with Sybase as well with PDO_ODBC. What you need is the following:
Must have Sybase ASE ODBC Driver which comes with the SDK. Find below the connection string used:
$connStr = "odbc:Driver={Adaptive Server Enterprise};server=".$myServer.";port=".$myPort.";db=".$myDB;
$conn = new PDO($connStr,$myUser,$myPass);
So i finally managed to connect to four database here's how I managed:
MySQL using PDO_MYSQL extension seemed to be installed on xampp by default didn't have to do much work. Here is the code I used for the connection:
$connStr = "mysql:host=".$myServer.";dbname=".$myDB;
$conn = new PDO($connStr,$myUser,$myPass);
Microsoft SQL Server using PDO_SQLSRV followed the instructions on http://craigballinger.com/blog/2011/08/usin-php-5-3-with-mssql-pdo-on-windows/. Here is the code I used:
$connStr = "sqlsrv:Server=".$myServer.";Database=".$myDB;
$conn = new PDO($connStr,$myUser,$myPass);
Oracle with PDO_OCI. Download and install the proper Oracle Instant Client on your windows machine for example instantclient_12_1 and add its path to PATH in SYSTEM Environmental Variables. Note Oracle supports only 2 versions down so select your client version properly. Do that and then restart your Apache. Here is the code I used:
$tns = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$myServer.")(PORT = 1521)))(CONNECT_DATA=(SID=".$myDB.")))";
$connStr = "oci:dbname=".$tns;
$conn = new PDO($connStr,$myUser,$myPass);
Sybase with PDO_ODBC Must have Sybase ASE ODBC Driver which comes with the SDK. Here is the code I used:
$connStr = "odbc:Driver={Adaptive Server Enterprise};server=".$myServer.";port=".$myPort.";db=".$myDB;
$conn = new PDO($connStr,$myUser,$myPass);
Related
I was struggling to connect my Drupal (7.43) application (hosted on a PHP 5.4 server) to a Microsoft Azure SQL database.
I got really depressed and even found myself arguing with my company's DBA for why did you install this database on the newest version of SQL Server?.
Edited:
The reason why I asked that was because of Microsoft official documentation which says one should not connect to newer versions of SQL Servers if PHP server version is under 7.*.
System Requirements for the Microsoft Drivers for PHP for SQL Server
https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-ver15#driver-versions
According to this article, if PHP server version is 5.4, the official MS driver for such php server is 3.2 version. Therefore, if the SQL Server driver is 3.2, it should not connect to SQL Server version higher than 2014.
TL; DR;
I used FreeTDS to connect 5.4 PHP application to Azure SQL Server! Yaay
FreeTDS is re-implementation of C libraries originally marketed by Sybase and Microsoft SQL Server. It allows many open source applications such as Perl and PHP (or your own C or C++ program) to connect to Sybase or Microsoft SQL Server.
My operational system is CentOS 7.
I installed basic yum packages for http server and php database connection.
yum install httpd httpd-tools php php-common php-cli php-odbc php-pdo unixODBC unixODBC-devel
So far I understand:
PDO stands for PHP Data Objects.
ODBC stands for Open Database Connectivity -- which is a standard application programming interface for accessing database management systems.
Alright, I then installed FreeTDS:
yum install epel-release
yum check-update
yum install freetds freetds-devel
Then I had /etc/freetds.conf:
[MYCLIENT]
host = myclient.database.edtech.com
port = 6669
tds version = 8.0 # Btw, how important is this version for old PHP servers versus new SQL servers?
I also had /etc/odbcinst.ini
[FreeTDS]
Driver = /lib64/libtdsodbc.so.0
FileUsage = 1
Furthermore, I had /etc/odbc.ini:
[MSSQLServer]
Driver = FreeTDS # Yes, ODBC will use FreeTDS, I get it.
Description = MSSQL Server
Trace = Yes
Server = myclient.database.edtech.com
Port = 6669
TDS_Version = 7.1 # Shouldn't this be same as the version in /etc/freetds.conf?
Database = ApplicationDB
Conclusions
I hope this question helps others.
FreeTDS is a different driver
than Microsoft's.
PHP does not know about FreeTDS not FreeTDS knows
about PHP version.
As far as I understood from the answers, there is
a ODBC bridge/layer in between them.
Better use the newest FreeTDS
version to make sure the connection works.
You said
The reason why I asked that was because of Microsoft official documentation which says I cannot connect to newer versions of SQL Server if my PHP version is below 7.*.
and
Is this php 5.4 connection really supposed to work with newest SQL
Server despite the official Microsoft docs say it should not?
...but actually, the Microsoft documentation you're talking about doesn't say you can't connect to SQL Server from PHP 5.4.
They said you can't (or at least you are not supported to) do that by using the Microsoft Drivers for PHP for SQL Server - which is the specific product that documentation is talking about.
FreeTDS is a different driver. By replacing the driver, you've replaced the thing which Microsoft is saying you shouldn't use. AFAIK Microsoft have no involvement with FreeTDS, so what they support, and what their driver works with, is entirely up to them.
I am trying to connect Oracle 11gXE with php. For php I downloaded wamp64 which is a 64bit version and the Oracle 11gXE is also 64bit. I am using windows 10 also 64bit.
Now I never connected Oracle to php so after searching a lot I found OCI and PDO_OCI among which OCI is recommended.
Now following steps were mentioned everywhere.
Download OCI thin client zip from oracle's official website.
Extract the zip
Include the path in environment variable.
Enable extension = php_oci8_12c.dll (or what ever oracle version you have) in php.ini
then use this $con = oci_connect($username, $password, $connection_string);
Now I have followed the steps, but failed to connect.
Following code to establish connection.
<?php
$username = "ABCD";
$password = "1234";
$connection_string = "localhost/XE";
$con = oci_connect($username, $password, $connection_string);
if(!$con) {
echo "Faild to connect";
}
else {
echo "Success";
}
?>
Now it show this error
So then I again I search to resolve this. Now I have faced following problems.
In my php.ini I didn't have php_oci8_11g.dll, I had php_oci8_12c.dl
And also had php_oci8_12c.dll in /ext.
So I downloaded php_oci8_11g.dll and mention extension = php_oci8_11g.dll.
Then try to run the code again but didn't work. Then in an article in here in stackoverflow I saw 32bit thin client is working because they had oracle 32bit and also xampp 32bit, But I have Oracle 11gXE 64bit and wamp 64bit also. So I have downloaded 64bit thin client.
Even I cannot see the php_oci8_11g in php extension pane.
php extension pane
Please help me with this what to do?
There are a few things to watch out for:
You need to make sure you have the same 32 or 64 bit architecture for all of: PHP, the webserver, the Oracle client libraries, and (if you are using Instant Client) also the MS VS Redistributable. (You did install the redistributable when you tried Instant Client? Check the Instant Client download page for details)
The version of the Oracle client libraries you want PHP to use (either in the Oracle XE installation, or from Instant Client) must be in PATH (and before any other Oracle version). Clashes between multiple version of libraries on Windows are hard to control. (Linux is easier...)
The version of the Oracle client libraries should be equal to, or greater than, the Oracle client version that the extension was compiled with E.g. php_oci8_12c was compiled with Oracle client 12c (but which will allow it to connect to older and newer database versions) so Oracle 12.1 or 12.2 client libraries must be in PATH (before other Oracle versions)
If you are downloading extensions from PECL, you need to make sure you get the thread safe, or non thread safe version of the DLL to match your PHP. And it must be for the same version of PHP you are using.
I'm getting error of PDO cannot find driver using PDO in environment. I'm using xamp on a Mac.
I had got to know the problem and I've installed freetds using homebrew install, and had successfully connected to the azure mssql server using below command
tsql -H 234fddfg.database.windows.net -p 1433 -U dbuser -P db123!
but I don't know what to do next.
I've tried below php code but I'm still getting the same error.
$dbh = new PDO("sqlsrv:Server=localhost;Database=mydb", "dbuser", "db123");
Driver for Microsoft SQL Server for PHP is needed to be installed for this.
Follow this link:
https://www.microsoft.com/en-in/download/details.aspx?id=20098
Perform the following steps to download and install the Microsoft Drivers for PHP for SQL Server (example below for 3.2 version):
1. Download SQLSRV32.EXE to a temporary directory
2. Run SQLSRV32.EXE
3. When prompted, enter the path to the PHP extensions directory
4. After extracting the files, read the Installation section of the SQLSRV32_Readme.htm file for next steps
Now just add the following line to your PHP.ini (this is for the non-thread safe version of PHP, which you are most likely using when you have installed PHP to use IIS FastCGI, which we recommend):
extension=php_sqlsrv.dll
And Restart server
Have you installed PDO_DBLIB, it seems that in OSX, we need to leverage PDO_DBLIB as the pdo driver connecting to MS SQL, e.g.
$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname", "$dbuser","$dbpwd");
you can refer to Configure PHP environment on Mac to connect to SQL Server using PDO interface for more information.
My setup was working on Windows but I recently switched to Ubuntu 12.04 LTS and now it won't connect. When I load a page where I need to talk to SQL Server, I get this error:
Database driver Cake\Database\Driver\Sqlserver cannot be used due to a
missing PHP extension or unmet dependency
It is obvious that CakePHP can't find the SQL Server PDO driver.
I found many old tutorials to help me but I took the most recent (I want absolutely to be able to use PDO with my CakePHP website). This is the tutorial I followed.
Using the terminal, I can access the database with this command
sqlcmd -S my.sql.server.com -U username
What do I need to do to connect to this sql server database from my ubuntu install with CakePHP 3.x?
Update (October 17th 2016):
Microsoft recently released a preview version (on their msphpsql repo) of pdo_sqlsrv and sqlsrv for Linux. It worked successfully for me on a Ubuntu 16.04.1 LTS (Xenial Xerus) virtual machine running php7.
Original answer:
As #AD7six said:
CakePHP's sql server driver is for using PDO_SQLSRV; If you need to use ODBC, there isn't one in the core.
PDO_SQLSRV is working if you are on Windows. CakePHP has a driver that support it natively.
If you want to talk to a SQL Server Database from Linux, you have to use the ODBC driver. You can install the driver using this tutorial which helped me a lot. After that, you will be able to connect to your database using PDO (not CakePHP):
try {
$query = new PDO(
"odbc:Driver={SQL Server};Database=[Database name]; Server=[Hosame]",
"[Username]",
"[Password]");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Thanks everybody for your help. It could be great to find/code a CakePHP 3 database driver for odbc.
The new version of CakePHP supports PDO-sqlsrv which is new the MS driver that supports PHP 7 and higher.
http://php.net/manual/en/ref.pdo-sqlsrv.php
It also allows you to connect to Azure. Example:
//Establishes the connection
$conn = new PDO( "sqlsrv:server=$serverName ; Database = $database", $uid, $pwd);
I wanted to setup a database connection from PHP to SQL server 2012.I have a wamp server(64 bit) set up on a windows machine(64 bit) with PHP 5.5.12 and on the same machine I have SQL server 2012 installed.
Extracted sqlsrv drivers from official_link
Copied the extracted drivers php_sqlsrv_55_ts.dll, extension = php_pdo_sqlsrv_55_ts.dll to php\ext folder, and then changed the php.ini file to include the extensions
extension = php_sqlsrv_55_ts.dll; extension = php_pdo_sqlsrv_55_ts.dll;
Now I tried
<?php
phpinfo();
?>
I see the following information without any SQL server information in it.
Don't seem to have configured SQL server connection successfully. Could some one please guide me on what I am missing here.
64 bit WAMP server was not able to connect using the drivers extension = php_sqlsrv_55_ts.dll; extension = php_pdo_sqlsrv_55_ts.dll;
So, I installed a 32 bit version of the WAMP server and it works fine now.
Check the php error log (c:\wamp\logs\php_error.log).
I had the same setup (64bit WAMP/PHP 5.5.12) and same missing sqlsrv reference in phpinfo and I got this error in my log:
PHP Warning: PHP Startup: Unable to load dynamic library
'c:/wamp/bin/php/php5.5.12/ext/php_pdo_sqlsrv_55_ts.dll' - %1 is not a
valid Win32 application. in Unknown on line 0
The solution was to install the 64bit version of the sqlsrv drivers. I found the unofficial 64bit drivers through http://robsphp.blogspot.nl/2012/06/unofficial-microsoft-sql-server-driver.html
Warning: In my testing I found these 64bit PHP_PDO_SQLSRV extension 10 times slower than when using PHP_PDO_ODBC.
I am visiting this thread and i think this might help full for you and other with the same issue:
How can I install pdo_sqlsrv on my windows 2008 Server 2008 R2?
The PDO Extension is not the same as the native driver Microsoft is offering. For PDO you must enable
extension=php_pdo_mssql.dll
in your php.ini.
{Normally this file (php_pdo_mssql.dll) should be in your PHP extension-directory (C:...\php\ext). If it's not there you can download PHP from http://windows.php.net/download/ and just take the extension from a package there (take one that correspond with your PHP version of course)}.
Above is taken from PDO MSSQL Server - Driver not found
read for more details. i have the same issue foe linux and i have saved all the pages thats why i am quoting for you help.
if all above didn't work for you then:
On the php.net it is listed that
On Windows, PDO_ODBC is built into the PHP core by default. It is linked against the Windows ODBC Driver Manager so that PHP can connect to any database cataloged as a System DSN, and is the recommended driver for connecting to Microsoft SQL Server databases.
http://php.net/manual/en/ref.pdo-odbc.php
You can connect to mssql server using odbc drivers as i have never connect by my self from windows i use to do it through linux using freeTds, following pages might help you
http://craigballinger.com/blog/2011/08/usin-php-5-3-with-mssql-pdo-on-windows/
Connect PHP to MSSQL via PDO ODBC
PHP to SQL Server without ODBC or MSSQL support