Connect SQL server with PHP

Asked

Viewed 1,744 times

-1

I’m trying to connect a Sqlserver 2012 using php 5.3 with the driver sqlsrv, however even managing to connect with SQL management studio by php always return me the error

[Microsoft][SQL Server Native Client 11.0][SQL Server]'.

Code:

sqlsrv_connect('DANIEL-PC\SQLEXPRESS', array(
    'UID' => 'sa',
    'PWD' => '123456',
    'Database' => 'CTe'
));
var_dump(sqlsrv_errors());

And that’s always the way out

array
  0 => 
    array
      0 => string '28000' (length=5)
      'SQLSTATE' => string '28000' (length=5)
      1 => int 18456
      'code' => int 18456
      2 => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Falha de logon do usuário 'sa'.' (length=85)
      'message' => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Falha de logon do usuário 'sa'.' (length=85)
  1 => 
    array
      0 => string '42000' (length=5)
      'SQLSTATE' => string '42000' (length=5)
      1 => int 4060
      'code' => int 4060
      2 => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Não é possível abrir o banco de dados "Phoros_CTe" solicitado pelo logon. Falha de logon.' (length=143)
      'message' => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Não é possível abrir o banco de dados "Phoros_CTe" solicitado pelo logon. Falha de logon.' (length=143)
  2 => 
    array
      0 => string '28000' (length=5)
      'SQLSTATE' => string '28000' (length=5)
      1 => int 18456
      'code' => int 18456
      2 => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Falha de logon do usuário 'sa'.' (length=85)
      'message' => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Falha de logon do usuário 'sa'.' (length=85)
  3 => 
    array
      0 => string '42000' (length=5)
      'SQLSTATE' => string '42000' (length=5)
      1 => int 4060
      'code' => int 4060
      2 => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Não é possível abrir o banco de dados "CTe" solicitado pelo logon. Falha de logon.' (length=143)
      'message' => string '[Microsoft][SQL Server Native Client 11.0][SQL Server]Não é possível abrir o banco de dados "CTe" solicitado pelo logon. Falha de logon.' (length=143)
  • By default SQL Server disables the user sa and only authentication by Windows user accounts is enabled. Certainty: Is the user enabled? Not using SSMS (SQL Server Management Studio) in Windows authentication mode? The server authentication type is set to mixed?

  • I believe this should be your dev environment. In the production environment nay use the user sa. It is equivalent to root Mysql and this is a huge security breach in your application!

  • the user is enabled and the authentication mode this Mixed.

  • @gmsantos, why?

  • 1

    @Patrick some risks are: exposure of user authentication with all the privileges in the SQL Server instance, access to improper information, loss of traceability (who sees what?) . Suppose your application has a SQL Injection gap, which you think it can cause more impact: a credential with only the necessary permissions in a specific schema/database, or the user who can do everything in the database instance?

1 answer

0


<?php

$server = 'DANIEL-PC\SQLEXPRESS';
$port = '1443'; // porta padrão
$server = $port !== '1443' && is_string($port) ? $server .= ", $port": $server;
$database = 'CTe';
$user = 'sa';
$pass = '123456';

$conn = sqlsrv_connect($server, array('Database' => $database, 'UID' => $user, 'PWD' => $pass));

if($conn)
{
  // Sucesso ao se conectar
}
else
{
  // Falha ao se conectar
  echo "A conexão foi estabelecida.<br/>";
  die(print_r(sqlsrv_errors(), true));
}

// If you don’t connect, make sure the information: served name, user, password and

  • The connection port is unlikely to be the problem.

  • I appreciate the help, but this is the same thing I did, only you gave an organized, and I put only this q does not work, user and password are right I use it and connect normal by SSMS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.