Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cannot log in to SQL Server: Login failed for user

ErrorSQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'myuser'

Short answer

SQLSTATE 28000 with error 18456 means SQL Server refused the login. The message sent to PHP is vague on purpose. The real reason is in the SQL Server error log as a state number: wrong password, unknown login, SQL logins switched off, or a missing database. Find the state first, then fix that one thing.

  • Applies to SQL Server 2022, PHP 8.3, ODBC Driver 18 for SQL Server
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    SQL Server hides the reason from the client but writes it to its own error log. Open the log, find the entry for error 18456 and read its State number and Reason text.

    -- SQL Server Management Studio, Object Explorer:
    -- Management > SQL Server Logs > right-click > View > SQL Server Log
  2. 2Match what you see, then jump to its fix

    1. 1Error: 18456, Severity: 14, State: 8 (or State 7) and Reason: Password did not match that for the login providedThe password is wrong, or the login is disabledFix
    2. 2Reason: Could not find a login matching the name providedThe login does not exist on this serverFix
    3. 3Reason: An attempt to login using SQL authentication failed. Server is configured for Windows authentication only.SQL logins are not enabled on the instanceFix
    4. 4Reason: Failed to open the explicitly specified database 'mydb'The database named in the connection cannot be openedFix

What does the error look like?

  • SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'myuser'
  • Login failed for user '<user>'. Reason: Password did not match that for the login provided
  • Login failed for user '<user>'. Reason: Could not find a login matching the name provided
  • Login failed for user '<user>'. Reason: An attempt to login using SQL authentication failed. Server is configured for Windows authentication only.

Cause 1 of 4

The password is wrong, or the login is disabled

1Check

State 8 means the password is incorrect. State 7 means the login is disabled and the password is also wrong. Try the same credentials from a small PHP script with the values read from the environment, so you know whether the app config or the account is at fault.

php -r '$c = new PDO("sqlsrv:Server=" . getenv("SQL_SERVER") . ";Database=" . getenv("SQL_DATABASE") . ";Encrypt=true;TrustServerCertificate=false", getenv("SQL_USER"), getenv("SQL_PASSWORD")); echo "connected\n";'

2Fix

Use the correct password in the application config, or ask the SQL Server administrator to reset it. After you switch an instance from Windows-only to mixed mode, the sa login starts out disabled, which shows up as state 7; enable the login first.

# in the environment of the PHP process (not in git)
SQL_USER=myuser
SQL_PASSWORD=the-correct-password

3Verify

Run the connection test again. It prints connected.

php -r '$c = new PDO("sqlsrv:Server=" . getenv("SQL_SERVER") . ";Database=" . getenv("SQL_DATABASE") . ";Encrypt=true;TrustServerCertificate=false", getenv("SQL_USER"), getenv("SQL_PASSWORD")); echo "connected\n";'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

The login does not exist on this server

1Check

The user name is misspelled, or the app points at a different server than the one where the login was created. This is common when a development connection string reaches production. Print the server and user the process really uses.

php -r 'echo getenv("SQL_SERVER"), " / ", getenv("SQL_USER"), "\n";'

2Fix

Correct the user name or server in the connection settings. If both are right, ask the administrator to create the login on that server, then add a database user for it.

-- administrator, in the target database
CREATE USER [myuser] FOR LOGIN [myuser];

3Verify

Connect again from PHP with the corrected settings.

php -r '$c = new PDO("sqlsrv:Server=" . getenv("SQL_SERVER") . ";Database=" . getenv("SQL_DATABASE") . ";Encrypt=true;TrustServerCertificate=false", getenv("SQL_USER"), getenv("SQL_PASSWORD")); echo "connected\n";'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

SQL logins are not enabled on the instance

1Check

The instance accepts only Windows accounts, but PHP sends a user name and password. SERVERPROPERTY returns 1 when the instance is in Windows-only (integrated security) mode.

SELECT SERVERPROPERTY('IsIntegratedSecurityOnly') AS windows_auth_only;

2Fix

Change the instance to SQL Server and Windows Authentication mode (SSMS: right-click the server, Properties, Security page), then restart SQL Server. The other way out is to connect with a Windows login and remove the user name and password from the PHP connection.

-- SSMS: server Properties > Security
-- select "SQL Server and Windows Authentication mode"
-- then restart the SQL Server service

3Verify

After the restart, repeat the PHP connection with the SQL login.

php -r '$c = new PDO("sqlsrv:Server=" . getenv("SQL_SERVER") . ";Database=" . getenv("SQL_DATABASE") . ";Encrypt=true;TrustServerCertificate=false", getenv("SQL_USER"), getenv("SQL_PASSWORD")); echo "connected\n";'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

The database named in the connection cannot be opened

1Check

The Database value is misspelled, the database is offline, or the login has no user in it. States 38, 46 and 126 point at a database problem. With a case-sensitive collation, the case of the name matters too.

echo "$SQL_DATABASE"

2Fix

Fix the database name, ask the administrator to bring the database online, or create the database user for the login.

-- administrator, in the target database
CREATE USER [myuser] FOR LOGIN [myuser];

3Verify

Connect again naming that database.

php -r '$c = new PDO("sqlsrv:Server=" . getenv("SQL_SERVER") . ";Database=" . getenv("SQL_DATABASE") . ";Encrypt=true;TrustServerCertificate=false", getenv("SQL_USER"), getenv("SQL_PASSWORD")); echo "connected\n";'

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep server, database, user and password in one environment file per environment, so a development value never reaches production.
  • After you change the authentication mode or create a login, test it from PHP before you deploy.
  • Give each application its own login, mapped to only the database it needs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.