简体   繁体   中英

Failed login to MSSQL with PHP

I am trying to log into a local SQL database, but I keep getting an error about 'Login Failed'. This is the first time that I have tried to connect to a MS SQL server... normally I use MySQL. So, I am not even sure if I am doing this correctly.

Here is my connection code:

function dbConnection()
{
    $host = '127.0.0.1';
    $db = 'my_db';
    $user = 'dbuser';
    $pass = "my_pw";

    $serverName = "tcp:$host,1443";
    $connectionOptions = array(
        "Database"=>$db,
        "Uid"=>$user, 
        "PWD"=>$pass
    );

    $connection = sqlsrv_connect($serverName, $connectionOptions);
    if (!$connection) print_r(sqlsrv_errors());

    return $connection;
}

print_r(dbConnection());

Everytime I try this, I get:

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'dbuser'.

The username and password are correct, and the permission are set correctly. The port is correct, too. I can log into SSMS just fine, but not through PHP.

Any ideas?

Assuming that your mssql server instance is say sqlexpress..

Please use

$serverName = "serverName\\sqlexpress"; 

or if the port is 1443, then

$serverName = "serverName\\sqlexpress, 1443" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM