KaKa
KaKa

Reputation: 559

How to set charset to UTF8 in pdo oracle(oci)

Here is my php codes

$tns = "
 (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
      )
        (CONNECT_DATA =
          (SERVICE_NAME = myservice)
      )
 )";
$conn = new PDO("oci:dbname=" . $tns, 'user', 'pass');

When I try to set the charset to UTF8 like this:

$conn = new PDO("oci:dbname=" . $tns . ";charset=UTF8", 'user', 'pass');

will get: ERROR: SQLSTATE[HY000]: OCINlsCharSetNameToId: unknown character set name (/app/software/php-5.6.30/ext/pdo_oci/oci_driver.c:610)pdo is NULL

I have already set the NLS_LANG to AMERICAN_AMERICA.UTF8, but looks like my host machine need to restart, I can NOT do it now.

So, what should I do can change the charset to utf8 with pdo/oci connected to oracle database??

And I want:

  1. NOT modify the charset in oracle databse
  2. ONLY use pdo, not oci_connect
  3. NOT restart my host machine

Upvotes: 2

Views: 5823

Answers (1)

KaKa
KaKa

Reputation: 559

Takes me 4 hours to solve this:

  1. Modify the /etc/sysconfig/httpd file by command vim /etc/sysconfig/httpd
  2. Add the codes below at the end of this file

    export LD_LIBRARY_PATH=/app/oracle/orahome/lib
    export ORACLE_HOME=/app/oracle/orahome
    export NLS_LANG=AMERICAN_AMERICA.UTF8 
    
  3. restart apache by command service httpd restart

You have to do NOTHING about your pdo or oracle or host machine. If you don't know the variable's value, just echo $ORACLE_HOME in command line.

BTW,

All these three variables should be set in /ect/profile, and you'd better make them same in these two files.

Upvotes: 3

Related Questions