STT LCU
STT LCU

Reputation: 4330

DB2 how to specify connection charset using PDO

I'm looking for the DB2 translation of the following (and working) MySQL query:

SET NAMES 'UTF8'

I've connected to a DB2 using PDO_ODBC, and I can successfully query it, so it's not a connection problem.

The driver says that NAMES is an unexpected token, so I guess that there's another way to specify the connection charset.

I've searched stackoverflow but most of the answers are for mysql drivers, and I don't know what to search in the IBM documentation (which isn't the most friendly to navigate)

What I'm looking for is a SQL query that's acceptable by DB2 that has the same effect of MySQL's SET NAMES xxx

Upvotes: 1

Views: 3202

Answers (3)

fishfin
fishfin

Reputation: 1

I am using pdo_ibm and I found a way of correctly setting the charset as follows:

1) Make the following query through some manual means (DBeaver, etc.):

SELECT CODEPAGE FROM SYSCAT.DATATYPES WHERE TYPENAME = 'VARCHAR';

Make a note of the result. For example: 1208

2) You can now set this codepage by two different ways:

The PHP Way

putenv('DB2CODEPAGE=1208');

First use this, then instantiate the PDO object.

The OS Way

(taken from https://docs.oracle.com/cd/E12102_01/books/AnyInstAdm784/AnyInstAdmPreInstall19.html)

For Windows:

  • Navigate to Control Panel > System and click the Advanced tab. Click Environment Variables.
  • In System variables section, click New.
  • In the Variable Name field, DB2CODEPAGE.
  • In the Variable Value field, enter the value that was returned in Step 1.

For UNIX, set the variable as shown below:

setenv DB2CODEPAGE <DB2CODEPAGE value>

For example: setenv 1208.

You may need to reboot the machine after creating the variable.

Upvotes: 0

Ian Bjorhovde
Ian Bjorhovde

Reputation: 11052

The is no equivalent statement in DB2.

DB2 client applications run using the standard operating system locale settings. On UNIX/Linux this is set via the LANG environment variable (which may be overridden with various LC_* environment variables), and on Windows this is configured using the Control Panel.

On Linux/UNIX with PHP, this would mean that you would need to have the proper locale environment variables set up when your web server starts.

Notes:

  • DB2 provides the option to override any operating system or environment-level setting using the DB2CODEPAGE registry variable. This setting is at the DB2 client instance level, so it will affect any/all applications that connect using the DB2 client you've installed.

  • If the client application codepage as determined by LOCALE differs from the database codepage, be aware that the DB2 client will perform code page conversion between the client codepage and the database codepage in both directions.

Upvotes: 2

AngocA
AngocA

Reputation: 7693

The encoding in DB2 is set when the database is created. In order to insert the right characters in the database, it is necessary the application executes with the right encoding and that support it.

Upvotes: 0

Related Questions