A few days ago, a project database used MSSQL. Then I need to call the data in it for analysis. Because I am good at PHP, I want to use PHP directly to connect to MSSQL.
Generally, the database used by PHP is MySQL, so you need to install the extension first to make PHP support MSSQL, and Microsoft has the corresponding extension download.
The general steps are as follows:
1. Download the PHP extension for connecting to MSSQL: https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql -server-2017
Extension download address: https://docs.microsoft.com/zh-cn/sql/connect/php/download-drivers-php-sql-server?view=sql -server-2017
? You can see the extended version for each version. I downloaded the latest 5.6, because the PHP version uses 7.3.2
After downloading, copy the corresponding dll crisis to the ext directory of the php installation directory, and add extensions in php. ini:
extension=php_pdo_sqlsrv_73_nts_x64 extension=php_sqlsrv_73_nts_x64
2. After installing the extension, you also need to install the corresponding ODBC driver on the computer: https://docs.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql -server-2017
Download and install the driver according to the corresponding extension version.
3. Connect MSSQL
If Larave is used, set DB_CONNECTION to sqlsrv in. env.
If you use PHP connection directly:
$connectionInfo = array( "Database" => 'table', "UID" => 'sa', "PWD" => '123456' ); $conn = sqlsrv_connect('127.0.0.1', $connectionInfo); if ($conn) { $sql = "select * from users; $data = sqlsrv_query($conn, $sql); }