section and everything up until
* * @link * * @package Refined Magazine */ global $refined_magazine_theme_options; $refined_magazine_theme_options = refined_magazine_get_options_value(); ?> connect PHP to SQL server on Debian : Linux : Cloud Admin Club

connect PHP to SQL server on Debian

Spread the love
PHP doesn’t have a native driver to connect directly to SQL server (MSSQL) database. However, there are multiple ways to enable PHP to connect to MSQL. for example, you can achieve that by Installing ODBC on Linux or Microsoft native driver MSSQL for Linux. in short, This article explains both ways to connect PHP to SQL server on debian.
What is ODBC ? 
Open DataBase Connectivity is an interface to communicate with relational SQL-databases. It is built on top of Microsoft ODBC interface and therefore requires that you have an ODBC driver to the database that you want to connect to.
Why chose SQL Server instead of MySQL?

MySQL and SQL server provide high performance and scaling capabilities, however, SQL server ( MSSQL )has the advantage. For instance, according to this study, SQL Server consistently outperforms MySQL when it involves transactions on high volumes of data.

In addition, MySQL is considered to be Oracle’s “entry-level” database. In contrast, SQL server (MSSQL) is Microsoft’s flagship database. Usually, Oracle will Advice their customers to buy their flagship database Oracle Database.

Method 1: Install ODBC modules on Linux

This method will allow PHP to connect using the Linux ODBC interface to intermediate between PHP and SQL server. Next.
First and foremost, synchronize your local packages index files with their sources.
Then, Install ODBC interface packages.
Next, Install your programming language ODBC modules.

Method 2: Install SQL server native driver

This method will use Microsoft Native SQL server drivers to allow PHP connection to SQL server (MSSQL) instant.
In recent years Microsoft has a new attitude toward open-source other words, it exported many projects and software to open-source communities.

Enable Microsoft repository.

Yep, Microsoft now has a repository for Linux. So, to complete this step.

First, Add Microsoft packages authentication keys “gpg keys” to your system.

Then, Create a repository file.

Install SQL Server tools for Linux

Install PHP MSSQL Native Drivers
Needless to say, You need PHP packages to connect to your SQL server. therefore, you can install PHP from your OS repository. Read this article to get the latest PHP update from sury’s repository.

Install the ODBC driver for Debian by following the instructions on the Linux installation article.

Step 1. Setup Linux Locale.

Configuring OS locale correct will avoid warning messages and adjust PHP output to display correctly in a browser. For example, for the en_US UTF-8 locale, run the following commands:

You may need to add /usr/sbin to your $PATH, as the locale-gen executable is located there.

Step 2. Install Microsoft SQL Server drivers

The following commands customized for PHP 7.4 on debian that has multiple PHP versions. Thus you need to update it with your version

If there is only one PHP version in the system, then the last step can be simplified to phpenmod sqlsrv pdo_sqlsrv.

Step 3. Restart PHP-FPM if you have it.

As we are adding modules for php. therefore, you will need to restart FastCGI Process Manager service.

Step 4. Install Apache and configure driver loading

Step 5. Restart Apache and test the sample script

To test your installation, see Testing your installation at the end of this document.