Search code examples
phpsql-server-2008sql-authentication

SQL Server SQL Authentication


I want to connect to a sql Server.. Only it won't work.

I granted a user and role enough permissions. But what am I doing

<?php
   $serverName = "LERAARSKAMER01\SQLEXPRESS"; 
   $database = "sqlservertest";

   // Get UID and PWD from application-specific files. 
   $uid = "sqlAdmin";
   $pwd = "tester";

   try {
      $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd); 
      $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
   }

   catch( PDOException $e ) {
      die( "Error connecting to SQL Server" ); 
   }

   echo "Connected to SQL Server\n";

   $query = 'select * from dbo.users'; 
   $stmt = $conn->query( $query ); 
   while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){ 
      print_r( $row ); 
   }

   // Free statement and connection resources. 
   $stmt = null; 
   $conn = null; 
?>

This is a screenshot of the server...

What is wrong?


Solution

  • After searching for a while I found I have to set sql Authentication on.

    Microsoft SQL Managment Studio --> Database (right click-> properties->security)

    Then I enabled sa, changed the password and settings with this (password policy,...). It works also with sqlAdmin (the other user). Just with the same code..

    USE Master
    GO
    ALTER LOGIN test_must_change WITH PASSWORD = ‘samepassword’
    GO
    ALTER LOGIN test_must_change WITH
          CHECK_POLICY = OFF,
          CHECK_EXPIRATION = OFF;