日期语法无效,PostgreSQL

时间:2015-12-10 03:41:09

标签: postgresql date

我想查询列名的inputdate,输入:date。当我查询:

where (inputdate>='$VEStart' AND inputdate<='$VEEnd')

我得到了:

error : warning pg_query() query failed error invalid input syntax for date where (inputdate>='' AND inputdate<='').

但是当我尝试更换它时:

where (inputdate>='2015-12-01' AND inputdate<='2015-12-31')

有效。我认为这是变量的问题。所以我试图回应两个变量,但它们显示正确的值。这有什么不对吗?

1 个答案:

答案 0 :(得分:1)

只是为了给你一个超出评论范围的例子,使用类似的东西,并确保在将以下代码投入生产使用之前添加改进;也很好地测试它。

<?php

$VEStart = '2015-01-01';
$VEEnd = '2015-2-28';

// validate the dates
if (!isDateValid($VEStart)) {
    print "Invalid start date\n";
    return;
}
if (!isDateValid($VEEnd)) {
    print "Invalid end date\n";
    return;
}

// format the dates
$VEStart = formattedDate($VEStart);
$VEEnd = formattedDate($VEEnd);
echo sprintf ("all good - %s and %s\n", $VEStart, $VEEnd);

// see http://php.net/manual/en/function.pg-query-params.php
$sql = 'select ... where inputdate between $1 and $2';
$connection = pg_connect('...');
$result = pg_query_params($connection, $sql, array($VEStart, $VEEnd));
...more code...    

// ----

// add good phpdoc
// see how others add doc - http://stackoverflow.com/questions/1904214/what-is-the-proper-php-function-documentation-format
function formattedDate($date) {
    list($year, $month, $day) = explode('-', $date);
    return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
}

// add good phpdoc
function isDateValid($date) {
    list($year, $month, $day) = explode('-', $date);
    return checkdate($month, $day, $year);
}

?>