Detailed steps to install PHP to support MSSQL database in Ubuntu environment

PHPz
Release: 2024-02-29 10:40:01
Original
888 people have browsed it

Detailed steps to install PHP to support MSSQL database in Ubuntu environment

Detailed steps to install PHP to support MSSQL database in Ubuntu environment

When developing web applications, we often encounter the need to connect to Microsoft SQL Server (MSSQL) database Case. In the Ubuntu environment, to connect PHP to the MSSQL database, you need to install relevant software and configure appropriate settings. Next, we will introduce in detail the steps to install PHP to support MSSQL database in Ubuntu environment, and provide specific code examples.

Step 1: Install the FreeTDS package

First, we need to install the FreeTDS package. FreeTDS is a free and open source software used to establish MSSQL database connections. Run the following command in the terminal to install the FreeTDS package:

sudo apt-get update
sudo apt-get install freetds-dev freetds-bin tdsodbc
Copy after login

After the installation is complete, we need to configure FreeTDS to connect to the MSSQL database. Edit the FreeTDS configuration file /etc/freetds/freetds.conf and add the following content:

[MSSQLServer]
host = your_mssql_server_ip
port = 1433
tds version = 7.2
Copy after login

Replace your_mssql_server_ip with the IP address of your MSSQL database server.

Step 2: Install the ODBC driver

We also need to install the ODBC driver to connect to the MSSQL database. Run the following command to install the ODBC driver:

sudo apt-get install unixodbc unixodbc-dev
Copy after login

After the installation is complete, we need to configure the ODBC data source. Edit the ODBC configuration file /etc/odbc.ini and add the following content:

[MSSQLServer]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Server = your_mssql_server_ip
Port = 1433
Copy after login

Replace your_mssql_server_ip with the IP address of your MSSQL database server.

Step 3: Install PHP MS-SQL extension

Next, we need to install the PHP MS-SQL extension to implement PHP's support for MSSQL databases. Run the following command to install the PHP MS-SQL extension:

sudo apt-get install php-mssql
Copy after login

After the installation is complete, we need to enable the MS-SQL extension in the PHP configuration file. Edit the PHP configuration file /etc/php/7.x/apache2/php.ini (it will vary depending on your PHP version) and add the following content:

extension=msql.so
Copy after login

Save and Exit the configuration file and restart the Apache server:

sudo systemctl restart apache2
Copy after login

Step 4: Test the connection

Now, we can write a simple PHP script to test the connection to the MSSQL database. Create a file called test_mssql.php and add the following code:

<?php
$server = 'MSSQLServer';
$user = 'your_mssql_username';
$password = 'your_mssql_password';
$database = 'your_mssql_database';

$conn = odbc_connect($server, $user, $password);

if ($conn) {
    echo "Connected to MSSQL server successfully.";
    
    $query = "SELECT * FROM your_table";
    $result = odbc_exec($conn, $query);
    
    while ($row = odbc_fetch_array($result)) {
        print_r($row);
    }

    odbc_close($conn);
} else {
    die("Could not connect to MSSQL server.");
}
?>
Copy after login

Replace your_mssql_username, your_mssql_password, your_mssql_database# Replace ## and your_table with your actual MSSQL database username, password, database name, and table name.

Access the

test_mssql.php file in your browser and if everything is set up correctly, you will see the data retrieved from the MSSQL database.

Through the above steps, we successfully installed PHP in the Ubuntu environment to support the MSSQL database environment, and wrote a simple PHP script to connect and retrieve data in the MSSQL database. Hope this article helps you!

The above is the detailed content of Detailed steps to install PHP to support MSSQL database in Ubuntu environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!