1

first of all, im really noob.(sorry for that) secondly, iam trying to connect php to mssql server. thirdly, i already install sqlsrv, pdo_sqlsrv, and msodbcsql.msi but still get error message when trying to connect enter image description here enter image description here

my php version is 8.0.10, x64; mssql server 2012.

my code in php to test connection :

<?php
$serverName = "10.xxx.xx.148";
$connectionInfo = array( "Database"=>"zzzz", "UID"=>"ww","PWD"=>"123cccc");
$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));}
?>

and then i get error, and instruction to install msodbcsql.msi, i install it just like the instruction. but then show new error that iam unable to solved.

Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 1 => -2146893019 [code] => -2146893019 2 => [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted. [message] => [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted. ) 1 => Array ( [0] => 08001 [SQLSTATE] => 08001 1 => -2146893019 [code] => -2146893019 2 => [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection [message] => [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection ) )

additional info: when i connect dbeaver (different pc) to mssql server (same server) there is no problem.

thank you in advance

cadmad
  • 45
  • 1
  • 6
  • If you connect to machine external of PHP did you get cert error as well? – user3783243 Apr 01 '22 at 15:18
  • 1
    Did you check the [documentation](https://learn.microsoft.com/en-us/troubleshoot/sql/connect/error-message-when-you-connect) or this [Q&A](https://stackoverflow.com/questions/17615260/the-certificate-chain-was-issued-by-an-authority-that-is-not-trusted-when-conn)? – Zhorov Apr 01 '22 at 15:21
  • @Zhorov, i tried adding TrustServerCertificate=True, in Additional Connection Parameters. but when i try to connect from php host, the error still occur. – cadmad Apr 01 '22 at 15:32
  • @user3783243 sorry i dont get what you mean, iam mssql is really a stranger for me – cadmad Apr 01 '22 at 15:32
  • 3
    This behavior is well-documented in the latest major versions of ODBC and JDBC, e.g.: [ODBC Driver 18.0 for SQL Server Released](https://techcommunity.microsoft.com/t5/sql-server-blog/odbc-driver-18-0-for-sql-server-released/ba-p/3169228). The available options are shown at [Connection Options](https://docs.microsoft.com/en-us/sql/connect/php/connection-options). Setting `TrustServerCertificate` to `1` or `True` will accept SQL Server's self-signed certificate. Please [Edit](https://stackoverflow.com/posts/71709139/edit) your question to show your exact changes if you cannot get it to work. – AlwaysLearning Apr 01 '22 at 21:52
  • Aside... DBeaver probably works because it's using older drivers. The `Encrypt=True` breaking change occurred in ODBC 18 and JDBC 10.2. – AlwaysLearning Apr 01 '22 at 21:54
  • oh my god, im so stupid. i just need add TrustServerCertificate=>true in the php file connectioninfo array. thank you @AlwaysLearning and all for the help. am i better deleted this question? – cadmad Apr 02 '22 at 05:15

3 Answers3

3

Just in case anyone is wondering how to define the TrustServerCertificate to 1, this is how i did it, i added the parameter as a new array element as shown below

<?php
$serverName = "10.xxx.xx.148";
$connectionInfo = array( 
 "Database"=>"zzzz",
 "UID"=>"ww",
 "PWD"=>"123cccc",
 "TrustServerCertificate"=>true
);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
?>
1

I used ODBC Driver 17 instead of ODBC Driver 18 and problem solved. In my case I didn't need encryption so driver 17 was ok for me.

Zahra
  • 2,231
  • 3
  • 21
  • 41
0

I downgrade from ODBC Driver 18 to ODBC Driver 17. It's work in Laravel 9 and PHP 8.2

Budi Odank
  • 11
  • 2