Driver SQL Server and CharacterSet option

lorenzo
Member | 3
+
0
-

Hello.

I'm trying to build a script PHP to integrate with the Nette Database.
I'm using SQLSRV driver to connect to MS SQL database.

Using the SQLSRV library directly, I can pass the option “CharacterSet” in the information for the connection like in this example:

$this->CONNECTION = sqlsrv_connect(‘IP’, array(
‘UID’ ⇒ ‘username’,
‘PWD’ ⇒ ‘password’,
‘Database’ ⇒ ‘db name’,
‘CharacterSet’ ⇒ ‘UTF-8’
)) or die(sqlsrv_errors());

Instead, using PDO I must set an attribute like in this example:

$dsny = “sqlsrv:Server=xx1;Database=xx2”;
$usery = ‘xx3’;
$passwordy = ‘xx4’;
$dbhy = new PDO($dsny, $usery, $passwordy);
$dbhy->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);

Using the Nette Database (that use PDO), how I can set this attribute?

Thanks a lot.

Rick Strafy
Nette Blogger | 65
+
0
-

Look at Other settings https://doc.nette.org/en/configuring#…, under options: section you should be able to specify PDO::SQLSRV_ATTR_ENCODING

Last edited by Rick Strafy (2021-11-18 17:02)

lorenzo
Member | 3
+
0
-

Hello @RickStrafy

Thanks for your reply.

Do you have an example on how to integrate the configuration?

For now, I only call the class Connection(“sqlsrv:Server=xx1;Database=xx2”,“uid”,“pwd”,[“options”]) to instantiate the connection.

Thanks again.

Rick Strafy
Nette Blogger | 65
+
0
-

Hello, sure, and do you use it in nette framework right? Look at that link I have sent you and copy the database: section to your .neon config and replace dsn with your sqlsrv:Server=xx1;Database=xx2 and also fill out the credentials.

Next step is to inject the connection to your services or presenters https://doc.nette.org/…atabase/core#… via that __construct method in example with Model class, or inject it as a public property with @inject tag.

Last edited by Rick Strafy (2021-11-18 18:29)

Rick Strafy
Nette Blogger | 65
+
0
-

Or if you are using it standalone, just check the source code how it's parsed from configuration, in your case it's this line https://github.com/…xtension.php#L84 so 4. parameter in the constructor of Connection class will be array with key => value of that config, so

new Connection("dsn", "user", "pwd", [
    PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8
])
lorenzo
Member | 3
+
0
-

Hello @RickStrafy

Thanks again for your reply.

I am going to use your last suggestion.

Many thanks.