Msphpsql: pdo_sqlsrv_db_handle_factory: Unknown exception caught on Debian Jessie

Created on 21 Sep 2016  路  11Comments  路  Source: microsoft/msphpsql

I'm installing in a debian Jessie, php7.0-fpm | (4.0.4 - Linux - Preview) and when i run:
$db = new PDO("sqlsrv:server=srv-database", "sa", "mypass");
i recieved back:

class PDOException#2 (10) {
  protected $message =>
  string(54) "pdo_sqlsrv_db_handle_factory: Unknown exception caught"
  private $string =>
  string(0) ""
  protected $code =>
  int(0)
  protected $file =>
  string(17) "/var/tmp/test.php"
  protected $line =>
  int(3)
  private $trace =>
  array(1) {
    [0] =>
    array(6) {
      'file' =>
      string(17) "/var/tmp/test.php"
      'line' =>
      int(3)
      'function' =>
      string(11) "__construct"
      'class' =>
      string(3) "PDO"
      'type' =>
      string(2) "->"
      'args' =>
      array(3) {
        ...
      }
    }
  }
  private $previous =>
  NULL
  public $errorInfo =>
  NULL
  public $xdebug_message =>
  string(236) "
PDOException: pdo_sqlsrv_db_handle_factory: Unknown exception caught in /var/tmp/test.php on line 3

Call Stack:
    0.0001     347200   1. {main}() /var/tmp/test.php:0
    0.0001     347496   2. PDO->__construct() /var/tmp/test.php:3
"
  public $severity =>
  int(1)
}

I followed this link to install: https://github.com/Microsoft/msphpsql/wiki/Install-pdo_sqlsrv-for-PHP-7.0-on-Debian

Most helpful comment

@weboide @Crinsane @josevieirame I had the same error and I fix it by adding the locales.

apt-get install -y locales \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
&& locale-gen

All 11 comments

I'm facing the same problem, have also followed the instructions of the given link.

Got the same error with Ubuntu 16.04 (within docker), installed msodbc from the apt repo (https://blogs.msdn.microsoft.com/sqlnativeclient/2016/09/06/preview-release-of-the-sql-server-cc-odbc-driver-13-0-0-for-linux/) and installed pdo_sqlsrv from pecl as specified in the readme.

I got the same problem. I'm using the oficial docker image for php 7 fpm (which is using debian jessie) and I followed the part of guide from the wiki page

This is the Dockerfile

FROM php:7.0.10-fpm

MAINTAINER Daniel Noyola <[email protected]>

# install the PHP extensions we need
RUN apt-get update && apt-get install -y unixodbc libgss3 odbcinst devscripts debhelper dh-exec dh-autoreconf libreadline-dev libltdl-dev unixodbc-dev wget unzip \
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install pdo

# Compile odbc_config
RUN cd /usr/local/src/ && dget -ux http://http.debian.net/debian/pool/main/u/unixodbc/unixodbc_2.3.1-3.dsc \
    && cd unixodbc-2.3.1/ && apt-get update && dpkg-buildpackage -uc -d -us -B && cp ./exe/odbc_config /usr/local/bin/

## Microsoft ODBC Driver 13 for Linux
RUN cd /usr/local/src/ \
    && wget https://download.microsoft.com/download/2/E/5/2E58F097-805C-4AB8-9FC6-71288AB4409D/msodbcsql-13.0.0.0.tar.gz \
    && tar xf msodbcsql-13.0.0.0.tar.gz && cd msodbcsql-13.0.0.0/ \
    && ldd lib64/libmsodbcsql-13.0.so.0.0; echo "RET=$?" \
    && sed -i 's/$(uname -p)/"x86_64"/g' ./install.sh \
    && ./install.sh install --force --accept-license
#
RUN cd /tmp && wget https://github.com/Microsoft/msphpsql/releases/download/v4.0.5-Linux/Ubuntu15.zip \
    && unzip Ubuntu15.zip \
    && mv -v Ubuntu15/* /usr/local/lib/php/extensions/no-debug-non-zts-20151012/ \
    && rm /usr/local/lib/php/extensions/no-debug-non-zts-20151012/signature

RUN "extension=php_pdo_sqlsrv_7_nts.so" >> /usr/local/etc/php/conf.d/sqlsvr.ini

CMD ["php-fpm", "-F"]

EXPOSE 9000

These are the version of the unixODBC and ODBC Driver 13 for SQL Server inside the container

$ odbcinst --version
unixODBC 2.3.1
$ odbcinst -q -d -n "ODBC Driver 13 for SQL Server"

[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.0.so.0.0
Threading=1
UsageCount=1

I can provide more feedback if it is necessary

@weboide @Crinsane @josevieirame I had the same error and I fix it by adding the locales.

apt-get install -y locales \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
&& locale-gen

@danielnv18 Thanks a lot, it worked! I was able to make it work with a ubuntu:xenial docker image.

Closing as it seems like it is resolved. Feel free to re-open.

Wow, why does this work?

I have the same issue but on MacOS, I have no idea what to do. Any help?

What's worth noting also is that I just get this error when running in CLI, the PHP-FPM runs without errors...

I was able to solve the issue, I added the following lines to my .zshrc:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Glad to hear that you figured out how to fix the issues, @iluuu1994
Next time when you encounter any problem, please create a new issue, and if you like you can reference any old issue that is related.

Was this page helpful?
0 / 5 - 0 ratings