phppdocentosunixodbcpdo-odbc

Unable to connect to MSSQL PHP7 with PDO: unixODBC


Firstly, I apologize because I have minimal experience with Linux so I may just not be understanding something obvious. Ultimately, I am trying to query a MSSQL database via PHP.

I have installed freetds and unixODBC. I confirmed the setup with both tsql (freetds) and isql (unixODBC) and everything works. The problem is when I try to connect from PHP using pdo using the following: $db = new PDO('odbc:mssql', '$username','$password');

I receive a fatal PHP error: Uncaught PDOException: could not find driver...

Upon further inspection, I looked at my PHP info file and the available PDO drivers are: mysql and sqlite. How/where do I get the pdo odbc driver for PHP7? In the PHP documentation, it appears that the driver is included in the PHP source, but the only option I see is to use ./configure and from what I understand, that is only to compile from source.

Is there a way to add the driver to an already compiled version of PHP7? I feel like I am missing something stupid. Any help would be greatly appreciated!


Solution

  • If your OS is UBUNTU

    Install the unixODBC driver manager and Microsoft ODBC driver for Linux

    wget https://raw.githubusercontent.com/Microsoft/msphpsql/PHP-7.0-Linux/ODBC%20install%20scripts/installodbc_ubuntu.sh
    

    Run installer

    sh installodbc_ubuntu.sh
    

    Install PHP dependenceis

    apt-get install php-pear php-dev
    

    Install sqlsrv (check last version with pecl search sqlsrv)

    pecl install sqlsrv-4.0.5
    

    Install pdo_sqlsrv (check last version with pecl search sqlsrv)

    pecl install pdo_sqlsrv-4.0.5
    

    Load extensions

    echo "extension=sqlsrv.so" | sudo tee --append /etc/php/7.0/fpm/php.ini
    echo "extension=pdo_sqlsrv.so" | sudo tee --append /etc/php/7.0/fpm/php.ini
    

    Restart PHP-FPM

    service php7.0-fpm restart
    

    For windows https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017

    CentOS 7

    sudo su
    curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
    exit
    sudo yum update
    sudo ACCEPT_EULA=Y yum install -y msodbcsql mssql-tools unixODBC-devel 
    sudo yum groupinstall "Development Tools"
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    echo "extension=sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
    echo "extension=pdo_sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
    

    or via yum look; https://webtatic.com/packages/php70/