Connect to SQL Server from CakePHP 3 on Ubuntu 12.04 LTS - php

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);

Related

PHP PDO could'nt not find driver

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.

Connection Failed CakePHP 3 and MSSQL on Linux

I'm trying to connect a new project CakePHP v3 installed in a common LAMP on Ubuntu Server and the DataBase is a MSSQL 2008. I've tried to install the library php5-mssql but nothing changes. Any idea?
The error message received is:
CakePHP is NOT able to connect to the DataBase.
DataBase driver Cake\Database\Driver\Sqlserver cannot be used due to a missing PHP extension or unmet dependency
Thanks!
CakePHP does not have an ODBC driver at this time. You can connect to MSSQL servers using the SQLServer driver from a Windows server, but this issue (https://github.com/cakephp/cakephp/issues/6990 ) just keeps getting bumped.
Since Cake3 uses PDO drivers to connect SQL Server (from Linux), you should try to install Microsoft ODBC Driver for Linux, see
http://onefinepub.com/2013/03/ms-sql-odbc-ubuntu/
it may helps

"connection rejected by remote interface" connecting to Firebird 3 with PDO

Try code below, but cause exception - SQLSTATE[HY000] [335544421] connection rejected by remote interface:
try {
$dbh = new PDO("firebird:dbname=localhost/3050:empty", "SYSDBA", "masterkey");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->query('SELECT idmspos, idmsqnt, cdmsval from svc$dms');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()) {
echo $row['idmspos']." ".$row['idmsqnt']." ".$row['cdmsval']."<br>";
}
$dbh = null;
}
catch(PDOException $e) {
$dbh = null;
echo $e->getMessage();
}
Specs:
Linux 3.2.0-4-686-pae #1 SMP Debian 3.2.65-1+deb7u1 i686 GNU/Linux
PHP 5.6
php5-interbase
Firebird 3 from official site. Server is worked, can connect to him from local and remote machine.
Find firebird.conf, uncomment (if using fbclient 2.x) and change the following parameters to disable WireCrypt and set higher priority for legacy authentication:
WireCrypt = Disabled
AuthServer = Legacy_Auth, Srp, Win_Sspi
AuthClient = Legacy_Auth, Srp, Win_Sspi
May be a bit late for the questioner but the "connection rejected by remote interface" might still be current topic as there was a change in the firebird api from debian 8 to 9/10.
The problem does also depend on which interbase/firebird-client-library to use. While in debian 8 one has libfbclient2 in Firebird API Version 2.5, starting from Debian 9 this libfbclient2 been upgraded to Firebird API 3.0. libFBclient ist the client-library to connect to Firebird / Interbase-Servers.
If, now, you have an old-versioned server, as we do, namely Interbase XE3 from year 2012, the new firebird-library in version 3.0 can not connect anymore to this old server type, while the previous library 2.5 can.
So one solution for us when migrating from Debian 8 to Debian 10 was to install the old version of libfbclient2 from debian 8 to the new Debian 10 system and then, afterwards, install the higher language clients, e.g. php7.3-interbase.
When installing the system in this manner, the php 7.3. interbase client shows no problems connecting the the old interbase server.
Here is a small instruction on how to install libfbclient2 (api version 2.5 from debian 8) to a new debian 9/10 system and to use it with php7.3:
First, uninstall all higher level language client packages, like php7.3-interbase and all firebird common/client/server-stuff.
Then download and first install via dpkg calls the old libfbclients from debian 8. They can be found hier:
https://packages.debian.org/jessie/libfbclient2
However, this package depends on the 4 following packages:
multiarch-support
libc6
firebird2.5-common
firebird2.5-common-doc
While the first two ones are in suitable version already available in Debian 10 and could be installed right from there, the latter two need to be downloaded from the debian 8 jessie - archive and need to be installed first.
You need to change the Firebird service as follows:
Install Firebird
Go search Services-> Firebird
Right click on Firebird->Properties-> LogOn-> and tick checkbox having option as "Allows services to interact with Desktop"
and then click on Restart service
You are now able to connect with firebird successfully in case of Firebird 2.1.

PHP PDO installation on windows (xampp)

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);

Linux based PHP install connecting to MsSQL Server

What is the best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server.
The PHP will only ever run on a Linux box.
I've been trawling for the simplest answer for a while now.
Php 5.6
Ubuntu
sudo apt-get install php5.6-sybase freetds-common libsybdb5
AWS / Centos / Redhat
sudo yum install php56-mssql
After that, you can connect to the MsSql database through PHP with something like this:
<?php
$server = 'localhost';
$user = 'someUser';
$pass = 'somePassword';
$database = 'theDatabaseName';
try {
$pdo = new \PDO(
sprintf(
"dblib:host=%s;dbname=%s",
$server,
$database
),
$user,
$pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "There was a problem connecting. " . $e->getMessage();
}
$query = "SELECT * FROM TestSchema.Employees";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
var_dump($results);
You can troubleshoot MsSQL with something like this on the command line:
tsql -H your.server.name -p 1433 -U yourusername -P yourpassword -D yourdatabasename
To install tsql you can run this:
Installation of SQL binaries for testing on any PHP version
sudo apt install freetds-bin
Php 7+
Microsoft has native drivers we can use. Full instructions are here (Redhat / Ubuntu / Others).
That link also contains required steps to install Ms SQL server on your dev machine (working on Ubuntu, doesn't work on AWS, but you can just spin up an RDS instance there).
It also contains basic instructions on how to create test tables and data in the database, and PHP connectivity code.
You can also install the components for a newer version of PHP like this:
sudo apt-get install php7.2-sybase freetds-common libsybdb5
you must to install mssql driver for php on linux.
this is a best tutorial for you.
Try: For Ubuntu
sudo apt-get install php5-sybase php5-odbc freetds-common
Edit freetds.conf then connect MSSQL server with this PHP.
After searching and trying out the many suggestions here and in other posts, and spending a lot of time, a colleague suggested trying ADO.
As we already had an ADO enabled PHP install, it literally took less than 10 minutes to get up and running.
If your serious about connecting from Linux PHP to MS-SQL, consider ADO.
It's interesting to note that every answer here proposes one solution to the problem rather than answering the question asked which was which is the "best" solution. Unfortunately the OP forgot to tell us what the criteria were for "best".
Nobody so far has mentioned odbc. While this entails both configuring the odbc driver for connecting to the database and enabling the php extension, this method provides the best isolation of the php code from the underlying database making it easier to port the code later.
The microsoft provided drivers for php should give good compatibility but only support v7 up of php.
You may prefer to stick with the functionality provided by your Linux distro to ensure you have a supportable product and/or automated patch management. An alternative to odbc here may be the freetds driver but you didn't tell us anything about which Linux distro this is.
Get the sqlsrv extension from microsoft for php http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

Categories