Hello,
I have problem with installing sqlsrv. I am using Ubuntu 16,04 and PHP v. 7.0. I went through the installation as you describing without problem, but with your test php file I am getting this error:
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/www/html/web/testsql.php:17 Stack trace: #0 {main} thrown in /var/www/html/web/testsql.php on line 17
The only difference that I found is, that although I am using PHP 7.0 .so files are in 20160303 folder - I changed the lines in php.ini files, but it still doesnt work.
Could you please help me with this problem? Both php.ini are attached.
Thank you
D. Stanek
@stanedav You're receiving this error because the extension is not loaded, could you try this command?
echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
I run this command in /etc/php/7.0/apache2:
When I run it under my user account I get error:
bash:php --ini | grep "Loaded Configuration" | sed -e "s|.:s||": Permission denied
So I log into root accout by sudo -i command then I get empty output:
root@dee-desktop:~# echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
root@dee-desktop:~#
Let's try the commands below, to check if sqlsrv is been loaded or not, and where sqlsrv.so resides
php -mfind /usr/lib/php -name sqlsrv.so1)
dee@dee-desktop:/etc/php/7.0/apache2$ php -m
[PHP Modules]
calendar
Core
ctype
date
dom
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mbstring
mcrypt
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlsrv
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zlib
[Zend Modules]
Zend OPcache
2)
dee@dee-desktop:/etc/php/7.0/apache2$ find /usr/lib/php -name sqlsrv.so
/usr/lib/php/20160303/sqlsrv.so
Awesome I can see the sqlsrv in the list of loaded drivers, Could you restart the apache by sudo service apache2 restart and run the php script again?
Still the same error...
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/www/html/web/testsql.php:17 Stack trace: #0 {main} thrown in /var/www/html/web/testsql.php on line 17
Could you run testsql.php through command line by running php /var/www/html/web/testsql.php?
Also can you move the testsql.php to default localhost /var/www/html, restart apache sudo service apache2 restart and then add this line to the php script?
<?php
print_r(get_loaded_extensions());
?>
Here is output:
Array ( [0] => Core [1] => date [2] => libxml [3] => openssl [4] => pcre [5] => zlib [6] => filter [7] => hash [8] => Reflection [9] => SPL [10] => session [11] => standard [12] => apache2handler [13] => mysqlnd [14] => PDO [15] => xml [16] => calendar [17] => ctype [18] => curl [19] => dom [20] => mbstring [21] => fileinfo [22] => ftp [23] => gd [24] => gettext [25] => iconv [26] => imap [27] => intl [28] => json [29] => exif [30] => mcrypt [31] => mysqli [32] => pdo_mysql [33] => pdo_pgsql [34] => pdo_sqlite [35] => pgsql [36] => Phar [37] => posix [38] => readline [39] => shmop [40] => SimpleXML [41] => sockets [42] => sqlite3 [43] => sysvmsg [44] => sysvsem [45] => sysvshm [46] => tokenizer [47] => wddx [48] => xmlreader [49] => xmlwriter [50] => xsl [51] => zip [52] => Zend OPcache )
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /var/www/html/testsql.php:18 Stack trace: #0 {main} thrown in /var/www/html/testsql.php on line 18
no sqlsrv in list...
is this the result of running the script through command line or apache? I am gonna set up a clean VM and go through the steps, meanwhile could you confirm if you have multiple php installed in different locations?
this is output of edited .php file, you will find it in attachment.
testsql.txt
Yes, I have multiple installations, because before I found this GitHub I found many guides how to install sqlsrv and it needed php 5.6... If I list my /etc/php there are three folders: 5.6 7.0 7.1,...
I am wondering, I dont have any data on my server, and I already set up SSH, maybe I can send you credentials via PM?
I have a hunch that apache is using a different php than what we just configured to load the driver, let's try an experiment, please run:
1.sudo php -i | grep 'Configuration File'
php -vphpinfo(); function to php script , and then run apache, you'll get a page with blue background, can you take an snapshot of the header and the first table?root@dee-desktop:~# php -i | grep "Configuration File"
Configuration File (php.ini) Path => /etc/php/7.1/cli
Loaded Configuration File => /etc/php/7.1/cli/php.ini
root@dee-desktop:~# php -v
PHP 7.1.2-4+deb.sury.org~xenial+1 (cli) (built: Mar 2 2017 10:39:59) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.2-4+deb.sury.org~xenial+1, Copyright (c) 1999-2017, by Zend Technologies

(pdf with first part of webpage:)
phpinfo().pdf
Cool! that's the issue, apache is using php 7.0 with extensions in 20151012 folder, but the configured php is 7.1. either you should configure and load sqlsrv to php.ini file for php 7.0 or load php 7.1 module for apache depending on which php version you wanna use. any preferences?
Nice I see the problem! I dont have any preference, both are acceptable for me.... So how can I change it to proper setup? Thanks
Could you follow instructions for the specific php version you wish to run in (https://github.com/Microsoft/msphpsql/#install-linux ) the instruction is separated for PHP 7.0 and 7.1., let me know how it goes.
I followed instructions, both 7.0 and 7.1 (the only thing that I did more was removing lines from php.ini in 7.0 with extensions and then I added by echo right one)...
Here is terminal output of whole installation...
Still getting same error...
@stanedav I went through the log you sent and did the exact steps, but I was unable to get apache run php7.0 and commandline use php 7.1, assuming that php 7.1 is what we want to run on apache, let's clean up php7.0 dependencies that might be installed from php 7.0 installation
sudo su
apt-get purge libapache2-mod-php7.0
apt-get purge apache2
apt-get purge php.7.0
apt autoremove php7.0-fpm
sudo su
add-apt-repository ppa:ondrej/php
apt-get update
apt-get -y install php7.1 mcrypt php7.1-mcrypt php-mbstring php-pear php7.1-dev
````
3. install apache and PHP 7.1 apache module
sudo apt-get install libapache2-mod-php7.1
sudo apt-get install apache2
4. Configure ini
echo "extension=/usr/lib/php/20160303/sqlsrv.so" >> /etc/php/7.1/apache2/php.ini
echo "extension=/usr/lib/php/20160303/sqlsrv.so" >> /etc/php/7.1/cli/php.ini
5. restart apache
sudo service apache2 restart
```
Hello, thank you very much, error is gone now! But.. I am getting this error in testsql.php:
"db", "Uid" => "user", "PWD" => "pass" ); //Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); //Select Query $tsql= "SELECT @@Version as SQL_VERSION"; //Executes the query $getResults= sqlsrv_query($conn, $tsql); //Error handling if ($getResults == FALSE) die(FormatErrors(sqlsrv_errors())); ?>
Results :
"); } sqlsrv_free_stmt($getResults); function FormatErrors( $errors ) { /* Display errors. */ echo "Error information:
"; foreach ( $errors as $error ) { echo "SQLSTATE: ".$error['SQLSTATE']."
"; echo "Code: ".$error['code']."
"; echo "Message: ".$error['message']."
"; } } ?>
Maybe just syntax error?
@stanedav Seems there are pre-configuration for apache and php which affects the end results, and is not related to PHP SQLSRV drivers. I recommend to try the install instructions (https://github.com/Microsoft/msphpsql/tree/dev#install-unix) on clean machine.
Hello, thank you for answer, I made clean installation, installed
everything in instructions (with php 7.0), but I am still getting the
error...
2017-03-11 2:48 GMT+01:00 Hadis Kakanejadi Fard notifications@github.com:
@stanedav https://github.com/stanedav Seems there arepre-configuration
for apache and php which affects the end results, and is not related to PHP
SQLSRV drivers. I recommend to try the install instructions (
https://github.com/Microsoft/msphpsql/tree/dev#install-unix) on clean
machine.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Microsoft/msphpsql/issues/318#issuecomment-285830246,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ANrs49HSdjkowlFywPqSsmu-iE6h0vCEks5rkf1lgaJpZM4MV5x7
.
@stanedav Could you give me these results of php7.0 installed on a clean machine:
The error you are receiving
using command line
php -i | grep "Configuration File"php -m | grep sqlsrvsqlsrv_connect() on on _command line_ using php var/www/html/testsql.phpApache
phpinfo()
<?php
phpinfo();
?>
The set up guide needs updating. I managed to get it working on CLI and apache by adding the following lines:
extension=sqlsrv.so
to
/etc/php/7.0/cli/php.ini
and
/etc/php/7.0/apache2/php.ini
@GDIBass thanks!
@stanedav Can you please try your steps on a fresh machine if possible and also answer @Hadis-Fard's questions?
@meet-bhagdev Yes, that's how I solved it. I set up a fresh Ubuntu VirtualBox/Vagrant installation and was trying to solve this issue. Adding the extension in the format mentioned fixed it for my installation, and it seems to be working great now. I am actually NOT the OP, but I was having the same issue so I figured I'd post how I fixed it. FWIW I installed every php extension I could find.
Hello guys, at first thank you all for helping and sorry for delayed answer, it was busy week. I tried to run clean machine again - completely reinstalled ubuntu...
First step was running all steps in https://github.com/Microsoft/msphpsql for Ubuntu 16.04 and for PHP7.0...
Copy of all terminal output during installation:
terminal_output_installation.txt
Then, I added lines to both .ini files (in cli folder and in apache2 folder):
extension=sqlsrv.so
display_errors = on
display_startup_errors = on
Both files are here:
php_apache2.ini.txt
php_cli.ini.txt
Then I created in /var/www/html two files, phpinfo.php and testsql.php...
For phpinfo.php:
phpinfo.php.txt
I opened it in web browser - page is empty...
from command line - php phpinfo.php, output is here:
For testsql.php:
testsql.php.txt
"db", "Uid" => "user", "PWD" => "pass" ); //Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); //Select Query $tsql= "SELECT @@Version as SQL_VERSION"; //Executes the query $getResults= sqlsrv_query($conn, $tsql); //Error handling if ($getResults == FALSE) die(FormatErrors(sqlsrv_errors())); ?>
Results :
"); } sqlsrv_free_stmt($getResults); function FormatErrors( $errors ) { /* Display errors. */ echo "Error information:
"; foreach ( $errors as $error ) { echo "SQLSTATE: ".$error['SQLSTATE']."
"; echo "Code: ".$error['code']."
"; echo "Message: ".$error['message']."
"; } } ?>
@Hadis-Fard output of commands that you send me is here:
terminal_1and2.txt
We made slight changes to the Install Instructions. See https://github.com/Microsoft/msphpsql/tree/dev#install-unix
@stanedav, I tried install instructions on a clean Ubuntu 16.04 machine, they seem to be fine.
Looks like you do not properly enable PHP for Apache and Apache displays the source code instead of executing it. Please try the updated Install Instructions.
Note that, you should have php.7.0.load and php.7.0.conf files in /etc/apache2/mods-enabled folder after you complete the installation steps.
So we can actually work off something scriptable & reproducible, here's what you are saying is the latest installation instructions (apt-transport-https is required for mssql-tools & the rest are other PHP extensions I require for our sites). Assuming this is run as a script by root:
apt-get update && apt-get upgrade -y
apt-get -y install php7.0 mcrypt php7.0-mcrypt php-mbstring php-pear php7.0-dev php7.0-xml re2c gcc g++ git apt-transport-https php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php7.0-xmlrpc php7.0-zip php7.0-soap php7.0-json php7.0-ldap unattended-upgrades unzip clamav
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql mssql-tools
sudo apt-get install -y unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
apt-get -y install libapache2-mod-php7.0
apt-get -y install apache2
echo "extension=sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=pdo_sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
sudo service apache2 restart
That results in PHP being rendered as text.
I tried enabling the PHP7.0 module for apache by doing
a2enmod php7.0
sudo service apache2 restart
Which fails. Here is the error log for it
root@scw-74e3fc:~# systemctl status apache2.service
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: failed (Result: exit-code) since Tue 2017-03-28 19:03:49 UTC; 7s ago
Docs: man:systemd-sysv-generator(8)
Process: 10268 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 10368 ExecStart=/etc/init.d/apache2 start (code=exited, status=1/FAILURE)
Mar 28 19:03:49 scw-74e3fc apache2[10368]: * The apache2 configtest failed.
Mar 28 19:03:49 scw-74e3fc apache2[10368]: Output of config test was:
Mar 28 19:03:49 scw-74e3fc apache2[10368]: [Tue Mar 28 19:03:49.746893 2017] [:crit] [pid 10377:tid 139958967220096] Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.
Mar 28 19:03:49 scw-74e3fc apache2[10368]: AH00013: Pre-configuration failed
Mar 28 19:03:49 scw-74e3fc apache2[10368]: Action 'configtest' failed.
Mar 28 19:03:49 scw-74e3fc apache2[10368]: The Apache error log may have more information.
Mar 28 19:03:49 scw-74e3fc systemd[1]: apache2.service: Control process exited, code=exited status=1
Mar 28 19:03:49 scw-74e3fc systemd[1]: Failed to start LSB: Apache2 web server.
Mar 28 19:03:49 scw-74e3fc systemd[1]: apache2.service: Unit entered failed state.
Mar 28 19:03:49 scw-74e3fc systemd[1]: apache2.service: Failed with result 'exit-code'.
In the below condensed version of the original instructions, however apache is fully aware of php and returns the same error as reported by @stanedav above.
apt-get update && apt-get upgrade -y
apt-get -y install libapache2-mod-php7.0 apache2 php7.0 mcrypt php7.0-mcrypt php-mbstring php-pear php7.0-dev php7.0-xml re2c gcc g++ git apt-transport-https php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php7.0-xmlrpc php7.0-zip php7.0-soap php7.0-json php7.0-ldap unattended-upgrades unzip clamav
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql mssql-tools
sudo apt-get install -y unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
echo "extension=sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=pdo_sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
sudo service apache2 restart
You aught to get a script like this to work in a reproducible way before changing the ReadMe
@aspark21 Could you try the following commands and let me know if they fix the issue for you?
a2dismod mpm_event
a2enmod mpm_prefork
a2enmod php7.0
service apache2 restart
@aspark21 , the 2 scripts that you have posted are exactly the same, except you are missing the following line in the first one:
sudo pear config-set php_ini php --ini | grep "Loaded Configuration" | sed -e "s|.:s||" system
I am posting the link to the updated instructions again: https://github.com/Microsoft/msphpsql/tree/dev#install-unix. See "Install the Microsoft PHP Drivers for SQL Server" section.
They are different in the order apache2 & libapache2-mod-php7.0 are installed which you highlighted as being material and the way to fix the issue from occuring.
I was about to add that the clue is probably here assuming this isn't out of date as that isn't covered in the current steps defined in the Readme
pecl install pdo_sqlsrv
echo -e "; priority=20\nextension=pdo_sqlsrv.so" > /etc/php/7.0/mods-available/pdo_sqlsrv.ini
phpenmod pdo_sqlsrv
Tried that didn't work.
Hi @stanedav, @aspark21,
I have added a few lines to the README file to ensure that, PHP is enabled with Apache.
https://github.com/Microsoft/msphpsql/tree/dev#install-unix
Please note that, the instructions have been tested on multiple clean Ubuntu 16.04 machines.
@ulvii I've added the changes you've made to the instructions in the below script
apt-get update && apt-get upgrade -y
apt-get -y install php7.0 mcrypt php-solr php7.0-mcrypt php-mbstring php-pear php7.0-dev php7.0-xml re2c gcc g++ git apt-transport-https php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php7.0-xmlrpc php7.0-zip php7.0-soap php7.0-json php7.0-ldap unattended-upgrades unzip clamav
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql mssql-tools
sudo apt-get install -y unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
apt-get install -y libapache2-mod-php7.0 apache2
a2dismod mpm_event
a2enmod mpm_prefork
a2enmod php7.0
echo "extension=sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=pdo_sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
sudo service apache2 restart
cat /var/www/html/sample.php <<"EOF"
<?php
$serverName = "yourServername";
$connectionOptions = array(
"Database" => "yourDatabase",
"Uid" => "yourUsername",
"PWD" => "yourPassword"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
//Select Query
$tsql= "SELECT @@Version as SQL_VERSION";
//Executes the query
$getResults= sqlsrv_query($conn, $tsql);
//Error handling
if ($getResults == FALSE)
die(FormatErrors(sqlsrv_errors()));
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['SQL_VERSION']);
echo ("<br/>");
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors )
{
/* Display errors. */
echo "Error information: <br/>";
foreach ( $errors as $error )
{
echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
echo "Code: ".$error['code']."<br/>";
echo "Message: ".$error['message']."<br/>";
}
}
?>
EOF
php /var/www/html/sample.php
Still getting the same error
PHP Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given in /var/www/html/test.php on line 13
Error information: <br/>SQLSTATE: IMSSP<br/>Code: -14<br/>Message: An invalid parameter was passed to sqlsrv_query.<br/>
However the problem is your sample app.....
Having now setup a "normal" application on such a server and the app connects to mssql via adodb mssqlnative just fine. Maybe drop the sample app if it's going to throw unhelpful errors....
Seems we've spent a lot of time going around in circles for nothing just because of that pointless sample app...
Hi @aspark21 ,
I am glad you managed to configure Apache.
In the below condensed version of the original instructions, however apache is fully aware of php and returns the same error as reported by @stanedav above.
Note that, Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() means the driver is not loaded.
sqlsrv_query() expects parameter 1 to be resource, boolean given means the driver is loaded, but the connection was not successful and sqlsrv_connect($serverName, $connectionOptions); returned false. Check Return Value section here.
I totally agree that, the sample app should not be executed further if the return value of sqlsrv_connect() is false.
Please consider adding the following lines after sqlsrv_connect() to get the error information about why the connection failed. I will add them to the Readme file too.
if( $conn === false ) {
die( FormatErrors( sqlsrv_errors()));
}
Hello guys, pretty rich conversation here, I tried updated readme, then added extension=sqlsrv.so to both ini files and now it works! Thanks for your effort!
Feel free to reopen the issue if you still need help.
thanks guys for sharing, i have same problm... php 5.5
Hi @Hadis-Fard,
I had a similar problem that @stanedav, but, with the follow differences: MacOs Environment and, in my case I have sqlsrv.so loaded. But with the same message: Call to undefined function mssql_connect().
Could you know how can I fix it?
@heberfomin mssql_connect() already removed, Please use sqlsrv_connect() instead.
arrrr!
@yitam thanks. Sorry for the stupid question!
/usr/bin/php7.0 -m
para ver los plugins
@wcadena Er, what is the problem? Please consider creating a new issue.
please state where I can get a sqlsrv.so file for php5.6 ubuntu 14.
@mhgill The sqlsrv driver on Linux only works for PHP 7+. I would suggest upgrading PHP - you can find PHP 7 packages for Ubuntu 14 here.
@stanedav I went through the log you sent and did the exact steps, but I was unable to get apache run php7.0 and commandline use php 7.1, assuming that php 7.1 is what we want to run on apache, let's clean up php7.0 dependencies that might be installed from php 7.0 installation
1. Clean upsudo su apt-get purge libapache2-mod-php7.0 apt-get purge apache2 apt-get purge php.7.0 apt autoremove php7.0-fpm1. install PHP 7.1sudo su add-apt-repository ppa:ondrej/php apt-get update apt-get -y install php7.1 mcrypt php7.1-mcrypt php-mbstring php-pear php7.1-dev1. install apache and PHP 7.1 apache modulesudo apt-get install libapache2-mod-php7.1 sudo apt-get install apache21. Configure iniecho "extension=/usr/lib/php/20160303/sqlsrv.so" >> /etc/php/7.1/apache2/php.ini echo "extension=/usr/lib/php/20160303/sqlsrv.so" >> /etc/php/7.1/cli/php.ini1. restart apachesudo service apache2 restart
Thank a lot. It helped me as well
Most helpful comment
The set up guide needs updating. I managed to get it working on CLI and apache by adding the following lines:
extension=sqlsrv.soto
/etc/php/7.0/cli/php.ini
and
/etc/php/7.0/apache2/php.ini