Linux based PHP install connecting to MsSQL Server - php

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

Related

configure pdo_dblib - freetds already installed

I am running a Linux server that already has freetds installed, but is not configured to support dblib. Only sqlite and mysql are currently supported for PDO. MSSQL support is also enabled.
If I want to add support for dblib and enable the PDO Driver for FreeTDS/Sybase DB-lib, do I just need to run this command on the existing install:
./configure --with-pdo-dblib
If so, from which directory would I run the command?
Environment:
PHP Version 5.5.38
CENTOS 6.9
Thank you
If you have configured FreeTDS correctly, you should be able to connect like so:
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");
If you can't, this answer gives more explicit instructions: Connect PHP to MSSQL via PDO ODBC
pdo_dblib is typically installed in most Linux distros when you install php-mssql (on CentOS and Ubuntu, for example). If the connect command isn't working, please rewrite your question to include the specific steps you have taken and the error output. Good luck!
Since writing this question, the support team at the company that is hosting this server installed the driver. I don't know exactly what steps they took to do it so I can not post a step by step answer as I would like. Closing this question.

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.

(Linux) PDO cannot find the MySQL driver

So basically I have been always developing with PHP under Windows with the WAMP package and I never had any trouble.
Now, I am trying to transition to Linux (Mint). Everything is well, except for the PDO extension which cannot find the MySQL driver. In fact, it states not having any available drivers at all.
First, I installed Apache:
sudo apt-get install apache2
Then, PHP:
sudo apt-get install php5
MySQL for PHP (I am not exactly sure this was required):
sudo apt-get install php5-mysql
Finally, the command line interface for PHP:
sudo apt-get install php5-cli
Note that I had installed MySQL a while ago for other purposes.
Everything else is working well. The server starts, runs, executes scripts. I am also able to create an instance of PDO, but then it throws a PDOException stating that it cannot find the MySQL driver. The code is typical of a PDO initialization :
$pdo = new PDO("mysql:host=127.0.0.1;db=testdb", "root", "");
I looked at the phpinfo() and for the value PDO I see no drivers. However, I see the line MySQL driver for PDO followed by the names of its authors, but I cannot tell if it guarantees the presence of the driver. Notice that I also see the authors of all the other drivers (SQLite, PostgreSQL, Oracle, etc.).
The command php -m (which, according to the php --help, lists all modules compiled with PHP) also clearly indicates that both PDO and pdo_mysql are installed.
My search returned a lot of results about the extensions pdo.so and pdo-mysql.so. I have been trying to reference them into the php.ini file without success. It would probably have a higher chance of success if I actually had these files, but so far I have been unable to find them.
So, here we are, about five hours later, with me being completely stuck.
Thank you.

Can't find MySQL driver anymore (Linux)

Today some of my packages in Ubuntu was upgraded automatically, and I didn't think of what was actually going on.
Ever since the update, my local dev-environment doesn't work anymore. First of by not working was mod_rewrite which I had to enable again using a2enmod. But now I've run into an issue that I can't seem to resolve. My application can't seem to find the PDO MySQL driver. When running the application, I get the error failed to open the DB connection: could not find driver.
This is strange, since if I check the phpinfo() the PDO drivers do support MySQL, and the socket path is a valid path.
pdo_mysql client API version is 5.5.35 according to php info.
PHP5: 5.5.3
MySQL: 5.5.35
Connectionstring
mysql:host=127.0.0.1;dbname=MyDB;port=3306
What could be causing this?
The PHP MySQL driver (mysql.so/mysqli.so) and the PHP PDO MySQL driver (pdo_mysql.so) are two separate modules. You need both of them for PDO functionality with MySQL.
It is quite possible that one of them is missing or of an incompatible version - I do not have an Ubuntu system at hand, but on my RPM-based Linux distribution there is a separate package for each module (php-mysql/php-mysqli and php-pdo_mysql). I also expect PDO to be using the newer mysqli.so driver, rather than the obsolete mysql.so one, so you should verify that one is installed as well.
Try this:
sudo apt-get install -y php5-mysql php5 mysql-client
This should automatically restart your apache if any of the dependencies aren't installed.
Try using vagrant.
Dependencies can be isolated, upgraded and downgraded when you like.
Vagrant

PHP PDO exception: could not find driver

Does the MySQL-server and PHP5-MySQLi version have to match in order for a connection to be possible? I'm currently receiving the error below: I am running BSD.
"Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'..."
Here is the the connection info:
$info = "mysql:dbname=myDB;host=localhost";
$user = "dbUser";
$pw = "somePW";
return(new PDO($info, $user, $pw));
Here is my MySQL information:
mysql-server-5.5.24
php5-mysqli-5.4.3
I had this same issue on my CentOS install. I had tried to install imagick and hosed my install. When I removed all of my php files and reinstalled something wasn't working right.
I ran:
yum install php-pdo
yum install php-pdo_mysql
After doing those two lines I ran
service httpd restart
and everything came back up and running.
PDO uses database specific drivers to connect to database systems. It looks like you are missing the pdo_mysql driver that is required to connect to a MySQL database. There is some details on installing the driver on the pdo_mysql manual page, or there may be a BSD package that you can use (I am afraid I'm not familiar enough with BSD to offer specific advice).
Thanks to zerkms and John C for pointing me in the right direction. Below are the commands I used to install the driver:
#cd /usr/ports/databases/php5-pdo_mysql
#make install clean
#apachectl restart

Categories