PDO sqlsrv is in phpinfo but does not work

Asked

Viewed 994 times

1

I’m trying to access an SQL Server server from another Ubuntu server 16.04 using PHP, I did the entire installation process of the drive informed by Microsoft’s own website and phpinfo is shown the drive installed as shown in the images, but when I connect to the bank is shown the error:

Error!: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired

phpinfo na seção do PDO phpinfo na seção do sqlsrv

PHP Version 7.1.3-2+deb.sury.org~xenial+1

Follow the connection code:

try {
    $db_host = 'HOST';
    $db_username = 'USER';
    $db_password = 'PASS';

    //$dsn = "mysql:dbname=test;host={$db_host}";
    $dsn = "sqlsrv:database=test;server={$db_host}";

    $conn = new PDO($dsn, $db_username, $db_password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
  • Are you using php7? could you put the connection code?

  • 1

    I am using PHP 7.1.3

  • If I’m not mistaken, it’s sqlsrv or odbc, use this here to see the prefixes: var_dump(PDO::getAvailableDrivers());

  • array(2) { [0]=> string(6) "sqlsrv" [1]=> string(5) "mysql" }

  • @Brunoagenor in your connection is mssql, the correct would be: new PDO("sqlsrv:Server=localhost;Database={$db_host}", "{$db_username}", "{$db_password}");. it is worth checking this out here also. And if you can remove the answer below, and necklaces here on the question would facilitate.

  • Really @Edilson, put the sqlsrv and now a different message is being shown. > Error!: SQLSTATE[IMSSP]: An invalid keyword 'dbname' was specified in the DSN string.

  • Does not write dbname, writ Database even.

  • 1

    Through the link you can see that and it was: $dsn = "sqlsrv:database=test;server={$db_host}"; But I get a new error message: > Error!: SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver 13 for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver 13 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712

  • Shows the error as if the extension was not installed, but in phpinfo shows otherwise

  • download the requested drive and see what gives.

  • The problem is that I already followed all these steps, at the end the link leads to this tutorial: https://download.microsoft.com/download/C/D/B/CDB0A3BB-600E-42ED-8D5E-E4630C905371/Linux_4.0_Install_Instructions.pdf

  • After a lot of research I realized that the folder /opt on the server had disappeared, I created it and installed the drive again, now another error is being shown: Error!: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired

  • I usually use freetds to connect to sqlsrv, I have a container that I use to run sqlsrv, this is the link to the dockerfile that I use to create the image https://hub.docker.com/r/toninho09/laravel/~/dockerfile/

  • I was able to connect, thank you very much to everyone who helped me. The latter was solved by specifying the SQL Server port that was not running on the default port: $db_host = 'HOST,PORT';

  • Okay, put the answer here to close.

Show 10 more comments

1 answer

1


Thank you very much to everyone who helped me here after recreating the folder /opt on the server (which by the way still not understood how it was deleted), and install again the drive that link helped me: https://blogs.msdn.microsoft.com/sqlnativeclient/2016/10/20/odbc-driver-13-0-for-linux-released/

After that the problem was that my SQL Server server was not using the default post, I used neststat to check the right port.
And that’s the final code:

try {
    $db_host = 'HOST,PORT';
    $db_username = 'USER';
    $db_password = 'PASS';
    $db_database = 'DATABASE';

    $dsn = "sqlsrv:database={$db_database};server={$db_host}";

    $conn = new PDO($dsn, $db_username, $db_password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

if ($conn) {
    echo "Connected!";
} else {
    echo "Error";
}

Browser other questions tagged

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