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
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
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
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
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
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
Save and Exit the configuration file and restart the Apache server:
sudo systemctl restart apache2
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."); } ?>
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.
test_mssql.php file in your browser and if everything is set up correctly, you will see the data retrieved from the MSSQL database.
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!