Created
November 16, 2017 19:16
-
-
Save ctorresr/ef75f0392d0e4102eda1326c6810c88d to your computer and use it in GitHub Desktop.
MSSQL PHP7 RECIPE FOR UBUNTU 16.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#RECIPE: MSSQL PHP 7.1 | |
#SOURCE: https://github.com/Microsoft/msphpsql/tree/dev#install-unix | |
#Ubuntu 16.04 | |
#STEP-1: Install PHP7+ | |
apt update | |
apt -y install php7.1 mcrypt php7.1-mcrypt php-mbstring php-pear php7.1-dev php7.1-xml | |
#STEP-2: Install Prerequisites | |
sudo su | |
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 | |
exit | |
sudo apt-get update | |
sudo ACCEPT_EULA=Y apt-get install msodbcsql mssql-tools | |
sudo apt-get install unixodbc-dev | |
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile | |
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc | |
source ~/.bashrc | |
#STEP-3:Install the Microsoft PHP Drivers for SQL Server | |
sudo pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system | |
pecl install sqlsrv | |
pecl install pdo_sqlsrv | |
#STEP-4: Install and Configure Apache | |
sudo su | |
apt-get install libapache2-mod-php7.1 apache2 | |
a2dismod mpm_event | |
a2enmod mpm_prefork | |
a2enmod php7.1 | |
echo "extension=sqlsrv.so" >> /etc/php/7.1/apache2/php.ini | |
echo "extension=pdo_sqlsrv.so" >> /etc/php/7.1/apache2/php.ini | |
#STEP-5: Restart Apache to load the new php.ini file | |
sudo systemctl restart apache2 | |
#STEP-6: CODE APP | |
<?php | |
$serverName = "yourServername"; | |
$connectionOptions = array( | |
"Database" => "yourDatabase", | |
"Uid" => "yourUsername", | |
"PWD" => "yourPassword" | |
); | |
//Establishes the connection | |
$conn = sqlsrv_connect( $serverName, $connectionOptions ); | |
if( $conn === false ) { | |
die( FormatErrors( sqlsrv_errors())); | |
} | |
//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/>"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment