Home  >  Q&A  >  body text

SQLSTATE: User login failed

<p>I am using the following PHP connection file to connect to my SQL Server database. </p> <pre class="brush:php;toolbar:false;"><?php $servername = "..."; $username = "..."; $password = ""; $dbname = "..."; try { $conn = new PDO("sqlsrv:Server=$servername;Database=$dbname", $username, $password); echo "Connected to $dbname at $servername successfully."; $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $pe) { die ("Could not connect to the database $dbname :" . $pe->getMessage()); } ?></pre> <p>I keep getting this error which reads: </p> <blockquote> <p>"SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server] Login failed for user $username. </p> </blockquote> <p>I have downloaded the latest versions of ODBC 17 and 18 which I hoped would resolve this issue but it does not. My SQL Server Authentication is also set to SQL Server and Windows Authentication. </p>
P粉107991030P粉107991030444 days ago448

reply all(1)I'll reply

  • P粉445750942

    P粉4457509422023-08-26 00:30:13

    If using laravel, in your .env file:

    DB_CONNECTION=sqlsrv
    DB_HOST=DESKTOP-91MB9QU\TESTSQLSERVER
    DB_PORT=
    DB_DATABASE=laravel_test
    DB_USERNAME=sa
    DB_PASSWORD='mypassword'

    You can use it. Note that DB_CONNECTION=sqlsrv, DB_HOST must be in the format of "PC-name\SQL-server-name", and DB_PORT must be empty.

    You can use it in a similar way as pure php code.

    reply
    0
  • Cancelreply