KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

PDO Connection failure SQLSRV charset update #3465

Open Andrew Guattery opened this topic on on Nov 24 - 1 comments

Andrew Guattery commented on Nov 24

For anyone getting the "names is not a recognized SET option" error when trying to connect to MSSQL via PDO here is the (temporary) fix: in Koolreport/core/src/datasources open PdoDataSource.php. Go to line 149 and replace this:

         if ($charset) {
            $this->connection->exec("set names '$charset'");
             }

with this:

        /*2025-11-24 changed sytax for PDO connections. original charset commented out below*/
        if ($charset) {
            $charset = "PDO::SQLSRV_ENCODING_".$charset;
            $this->connection->setAttribute(PDO::SQLSRV_ATTR_ENCODING, $charset);
    
        }

And you will be good to go.

Andrew Guattery commented on Nov 25

AN UPDATE: So the MSSQL driver has some changes that make this "not quite" right. First, if you read here: MSSQL BOL PHP Driver You will see that "charset" is not supported in the PDO driver. But then if you read HERE: Constants for MSSQL PDO Driver you see there are two charset options:

  1. PDO::SQLSRV_ENCODING_UTF8 Data is in the UTF-8 encoding. This is the default encoding.
  2. PDO::SQLSRV_ENCODING_DEFAULT Uses PDO::SQLSRV_ENCODING_SYSTEM if specified during connection. Use the connection's encoding if specified in a prepare statement. So default IS UTF8 or whatever the default is set to in config.

One other note: The

setAttribute

doesn't work well passing in the attribute as a parameter. better to hard-code:

$this->connection->setAttribute(PDO::SQLSRV_ATTR_ENCODING, $PDO::SQLSRV_ENCODING_UTF8);

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None