How can I alter NLS_DATE_FORMAT in PHP

自闭症网瘾萝莉.ら 提交于 2020-01-15 09:08:15

问题


How can I alter the NLS_DATE_FORMAT from an Oracle database using PDO. So far I have this:

    $date_set = "DD-MM-YYYY HH24:MI:SS";
    $query = $this->conn->prepare("ALTER SESSION SET 'NLS_DATE_FORMAT' = ?");
    $query->execute(array($date_set));

But when I try to fetch the session:

    $query = "select * from nls_session_parameters where parameter='NLS_DATE_FORMAT'";
    $parameters = array();
    $test = $this->fetchRow($query, $parameters);
    var_dump($test); exit();

it returns:

DD-MON-RR"


回答1:


This query:

ALTER SESSION SET 'NLS_DATE_FORMAT' = 'DD-MM-YYYY HH24:MI:SS'

... should trigger this error:

ORA-00922: missing or invalid option

If you cannot detect it, there's an issue in your error checking code. Most likely, you haven't configured PDO to throw exceptions.

The problem is that NLS_DATE_FORMAT is not a string but an identifier:

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY HH24:MI:SS';



回答2:


In my system, setting NLS_DATE_FORMAT have no effect, maybe because I used oracle instant client. The cure for my system is to add another alter session command :

  ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY HH24:MI:SS';
  ALTER SESSION SET NLS_DATE_LANGUAGE = 'AMERICAN';

It seems that the OCI driver got confused when NLS_DATE_FORMAT is set and NLS_DATE_LANGUAGE is unset, and reverting to the DD-MON-RR format.




回答3:


When using PHP/PDO/Oracle you can use:

//change settings below
$username='user';
$password='mypassword';
$dbname='databasename';
$hostname='localhost';
$port='1521';
$sid='XE'; //could also be ORCL
$charset='WE8ISO8859P1'; //or UTF8 or whatever charset
$oraclelangauge='AMERICAN';

$tns = "(DESCRIPTION = 
      (ADDRESS = (PROTOCOL = TCP)(HOST = ".$hostname.")(PORT = ".$port."))
      (CONNECT_DATA = (SID = ".$sid.")(SERVER = DEDICATED)))";

$dbDB = new PDO("oci:dbname=".$tns.";charset=".$charset, $username, $password);
$result = $dbDB->query("ALTER SESSION SET NLS_DATE_LANGUAGE = '".$oraclelanguage."'"); 

Be sure to have php_pdo_oci extension enabled and first double check your Oracle connection settings with a different Oracle client like SQL Developer.

Also first check if ALTER SESSION SET NLS_DATE_LANGUAGE = <language> is working, for the $oraclelanguage setting you have set, in an Oracle client.



来源:https://stackoverflow.com/questions/22860101/how-can-i-alter-nls-date-format-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!