Solution to the problem that php cannot connect to mssql: 1. Modify the authentication mode; 2. Modify and confirm the account password; 3. Enable the account's permission to access the database.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php cannot link to mssql?
php is usually used together with mysql database, but when developing some enterprise applications, because the original application of the enterprise uses MSSQL, php must be connected to SQL Server.
This is also a Big pitfalls, let’s talk about 3 common reasons why using php to connect to SQL Server fails...
Before connecting php to mssql, you need to install the sqlsrv extension first. This is also a lot of pitfalls.
php code to connect to SQL Server is as follows
//$serverName = "localhost"; $serverName = "127.0.0.1"; $connectionInfo = array( "Database"=>"qinziheng", "UID"=>"sa", "PWD"=>"123456"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); }
1. The authentication mode is wrong
SQL Server has two authentication modes,
1-1) windows authentication mode,
means using the account or domain account in the windows system.
1-2) Some SQL Server authentication modes are also called SQL Server and Windows authentication modes.
Use the account created by the SQL Server database.
It is recommended to use the second type. SQL Server authentication mode.
2. The account password is incorrect
When PHP connects to the msql database, it involves the windows system account, domain account and SQL Server database account. For friends who have connected once, I don’t know which one to choose.
When we use PHP to develop the mssql database, we use the SQL Server identity authentication mode in 1-2. The connection is successful using the SQL Server database account, but the domain account connection fails.
The relationship between the Windows system account and the SQL Server database account is just like the relationship between the Linux system account and the MySQL database account in Linux. You cannot use the www user in Linux to log in and connect to the MySQL database.
3. Unauthorized access to the database
Although your account password is correct, the PHP connection to mssql still fails. The reason is that your account does not have permission to access the database. At this time, you need to log in to SSMS (SQL Server Management Studio), authorize the user, or add the user to the corresponding security group.
There is another possible reason for the errorThe above three reasons are often encountered in development. If your PHP and SQL Server are installed on different hosts On the other hand, there is another possibility that SQL Server does not allow remote connections and access.
Recommended learning: "
PHP Video TutorialThe above is the detailed content of What to do if php cannot link to mssql. For more information, please follow other related articles on the PHP Chinese website!