【问题标题】:ORA-00932 inconsistent datatypes: expected DATE got NUMBERORA-00932 不一致的数据类型:预期 DATE 得到 NUMBER
【发布时间】:2017-12-28 17:37:51
【问题描述】:

我正在尝试输入用户的日期值,然后在查询中使用该值。

我的日期值是这样的 2017.07.21 08:59:26

$MYDATE= $data[$i]->MYDATE;
//ALSO I ADDED
$str = oci_parse($conn, "ALTER SESSION SET NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS'");
oci_execute($str);
//
$sql = 'INSERT INTO MYTABLE(ID,MYDATE)'.'VALUES(:ID,to_date(:MYDATE,\'YYYY.MM.DD HH24:MI:SS\'))';
$compiled = oci_parse($conn, $sql);
oci_bind_by_name($compiled, ':ID', $ID);
oci_bind_by_name($compiled, ':MYDATE', $MYDATE);
oci_execute($compiled);

它给了我这个错误:

不一致的数据类型:预期 DATE 得到 NUMBER

【问题讨论】:

  • 如果您描述 MYTABLE,打印 $MYDATE,然后打印 $ID,那么您很有可能会弄明白。
  • 好的,我打印我的日期变量结果是 2017.07.21 08:59:26
  • 编辑您的帖子,添加描述 MYTABLE 的输出,然后显示 $ID 的值。
  • 我已经提到过“我的日期值是这样的 2017.07.21 08:59:26”

标签: php sql oracle insert


【解决方案1】:

这个脚本:

<?php

/*

drop table mytable purge;
create table mytable (id number, mydate date);

*/

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$c = oci_connect("hr", "welcome", "localhost/XE");
if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}

$sql = <<<'END'
    INSERT INTO MYTABLE(ID,MYDATE) VALUES(:ID,to_date(:MYDATE,'YYYY.MM.DD HH24:MI:SS'))
END;

$id = 1;
//$mydate = '2017.07.21 08:59:26';  // also works
$mydate = date("Y.m.d H:i:s");
echo "input value: ", $mydate, "\n\n";

$s = oci_parse($c, $sql);
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}

$r = oci_bind_by_name($s, ':id', $id);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not bind a parameter: '. $m['message'], E_USER_ERROR);
}

$r = oci_bind_by_name($s, ':mydate', $mydate);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not bind a parameter: '. $m['message'], E_USER_ERROR);
}

$r = oci_execute($s, OCI_NO_AUTO_COMMIT);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}


// Fetch back the data

$s = oci_parse($c, "select to_char(mydate, 'YYYY.MM.DD HH24:MI:SS') as mydate from mytable");
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}

$r = oci_execute($s, OCI_NO_AUTO_COMMIT);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}

oci_fetch_all($s, $r);
var_dump($r);

oci_rollback($c);

?>

给出这个输出:

input value: 2021.04.06 10:50:20

array(1) {
  ["MYDATE"]=>
  array(1) {
    [0]=>
    string(19) "2021.04.06 10:50:20"
  }
}

你可以做的事情:

  • 在您输入的日期上输入var_dump(),以显示其类型
  • 在表格上做 DESC 以显示您的数据类型

我相信您添加它是为了进行测试,但在生产中您希望避免为每个连接运行 ALTER SESSION,因为这会增加开销。而是使用TO_DATE,或在 PHP 启动之前设置环境变量NLS_DATE_FORMATNLS_LANG(两者都是必需的)。请参阅The Underground PHP and Oracle Manual中的全球化章节

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2017-04-21
    • 2019-07-24
    • 2011-06-03
    相关资源
    最近更新 更多