简体   繁体   中英

mssql using pdo fail login getting Login failed for user 'dbo'

IN Microsoft SQL server Management Studio when I run

EXEC sp_helplogins @LoginNamePattern='mmanlapig\martin'

I get this :

   LoginName           DBName   UserName    UserOrAlias
1  mmanlapig\martin    homis    db_owner    MemberOf
2  mmanlapig\martin    homis    dbo         User    

Then in my code I have :

define('DBHOST', 'localhost\SQLEXPRESS'); 
define('DBNAME', 'homis'); 
define('DBUSER', 'dbo'); 
define('DBPASS', ''); 
$dbname = "`" . str_replace("`", "``", DBNAME) . "`";
$dbh1 = new PDO("odbc:Driver={SQL Server};Server=" . DBHOST . ";Database=$dbname;", DBUSER, DBPASS);

When I run this code

Select sp.name as LoginName, sp.type_desc as LoginType,
    dp.name as DBUser, dp.type_desc as UserType
from sys.server_principals sp
join sys.database_principals dp on dp.sid = sp.sid
where sp.name = 'mmanlapig\martin';

I get

   LoginName           LoginType        DBUser  UserType
1  mmanlapig\martin    WINDOWS_LOGIN    dbo     WINDOWS_USER
  1. Why do I get error saying that i fail in connecting to database when all my login credential is ok.
  2. What is the correct username password that I need to use to connect to database?

Tried these steps from here

The issue was that the server was set to "Windows Authentication Mode" only. To fix this I

  1. Right click the server - > Properties
  2. Click "Security" in the left side of the "Server Properties" dialog
  3. Changed server Authentication to "SQL Server and Windows Authentication mode"
  4. Clicked "OK"
  5. Restarted Associated services. At first I forgot to restart the services, so I was still getting the error, but now I am able to connect without an issue.

Still same issues

Hope this article helps you: Using PDO's odbc to connect to a MSSQL server instance with Windows Authentication

$conn = new PDO(
    'odbc:
        Driver=SQL Server;
        Server=MyServer\MyInstance;
        Database=MyDatabaseName;
        Trusted Connection=Yes;'
);

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