Search code examples
phpsql-serverwindowsmanagement-studio-express

PHP connect to SQL server with a windows account


I am trying to connect to a SQL server database with a windows account. In sql managment studio, I can open the database with this account but not with my php application. This user is not a sql user, but he have the right on the SQL database.

I tried with a sql user and it works with my php application.

Is it possible to use my windows account from the php application, and not a specifique sql user?


Solution

  • sqlsrv_connect

    By Default it trys to establish a Connection with a Windows Auth. u can try the snipped below. If u have to use a defined WindowsUser to Login, try the Code from the documentation link above.

    $serverName = "serverName\sqlexpress"; //serverName\instanceName
    
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"dbName");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    if( $conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }