Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php DB2将十进制转换为带破折号的字符串,然后转换为日期_Php_Mysql_Database_Db2 - Fatal编程技术网

Php DB2将十进制转换为带破折号的字符串,然后转换为日期

Php DB2将十进制转换为带破折号的字符串,然后转换为日期,php,mysql,database,db2,Php,Mysql,Database,Db2,我试图从db2表中获取列'extd2d',该表包含日期值,但它是一个压缩/十进制列 因此,对于2018-02-02,它返回20180202 我试图选择它来匹配mysql列,该列是返回2018-02-02的日期数据类型。我在想,为了匹配它们,我需要做的是将extd2d字段转换为带破折号的字符串,然后将其转换为日期 我目前正在这样做: select date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))

我试图从db2表中获取列'extd2d',该表包含日期值,但它是一个压缩/十进制列

因此,对于2018-02-02,它返回
20180202

我试图选择它来匹配mysql列,该列是返回2018-02-02的日期数据类型。我在想,为了匹配它们,我需要做的是将extd2d字段转换为带破折号的字符串,然后将其转换为日期

我目前正在这样做:

select date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))  as start_date,
from table1;
以正确的YYYY-MM-DD格式显示,但如果使用相同的行

date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))
在where子句中将其与mysql中的DATE列相匹配时,它表示无法对两者进行比较。我应该换一种方式吗?这样做更好还是从mysql日期中删除破折号并进行比较更好

编辑:

这就是我在where子句中进行比较的方式:

 AND date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2)) >= {$row['start_date']}
脚本:

$sql = "
    SELECT 
        sku_id,
        dealer_id,
        locations,
        s.sku_group_id as groupID,
        s.frame as frame,
        s.cover1 as cover,
        s.color1 as color,
        start_date - interval 7 day as start_date
    from placements p
    inner join skus s on p.sku_id = s.id
    where curdate() between p.start_date and p.expire_date
    group by sku_id, dealer_id
    limit 100";

$result = mysqli_query($conn,$sql);

while($row = mysqli_fetch_assoc($result)) 

    {
        $resultData[] = $row;

$sql2 = "
    SELECT
          framec,
          covr1c,
          colr1c,
          date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))  as start_date,
          sum(orqtyc) as TotalQTY
      from table1
        where cstnoc = {$row['dealer_id']}
        AND framec = {$row['frame']}
          AND colr1c = {$row['color']}
          AND covr1c =  {$row['cover']}
          AND date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2)) >= {$row['start_date']}
    group by framec,covr1c,colr1c,extd2d
";

$result2 = odbc_exec($DB2Conn, $sql2);

while($row2 = odbc_fetch_array($result2)){

        $db2Result[] = $row2;
}
}

print_r($resultData);
print_r($db2Result);

您的问题是,尽管DB2有一个本机日期格式,但实际上PHP没有。因此,当您在PHP中从MySQL中提取日期时,您将获得
YYYY-MM-DD
格式的字符串,然后将其与
DB2Date
字段进行比较。如果您根本不强制转换,并从MySQL字段中删除破折号,那会更好:

$sql2 = "
    SELECT
          framec,
          covr1c,
          colr1c,
          date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))  as start_date,
          sum(orqtyc) as TotalQTY
      from table1
        where cstnoc = {$row['dealer_id']}
        AND framec = {$row['frame']}
          AND colr1c = {$row['color']}
          AND covr1c = {$row['cover']}
          AND extd2d >= " . str_replace('-', '', $row['start_date']) . "
    group by framec,covr1c,colr1c,extd2d
";
真正的问题是,这取决于SQL注入,因此您不应该将值连接到SQL中,而应该使用如下参数标记:

$sql2 = "
    SELECT
          framec,
          covr1c,
          colr1c,
          date(substr(extd2d,1,4)||'-'||substr(EXTD2d,5,2)||'-'||substr(EXTD2d,7,2))  as start_date,
          sum(orqtyc) as TotalQTY
      from table1
        where cstnoc = ?
        AND framec = ?
          AND colr1c = ?
          AND covr1c = ?
          AND extd2d >= ?
      group by framec,covr1c,colr1c,extd2d
";
$parms = array(
    $row['dealer_id'], 
    $row['frame'], 
    $row['color'],
    $row['color'],
    intval(str_replace('-', '', $row['start_date']))
);
$prep_stmt = odbc_prepare($conn, $stmt2);
$success   = odbc_execute($prep_stmt, $parms);

数组中的参数标记
和替换数据可防止注入,因为SQL解析器不会将替换数据视为SQL语句的一部分。正如您所见,保护您的程序不受恶意用户的攻击并不十分困难,因此每次使用SQL时都应该这样做。

在同一
WHERE
子句中,您如何比较DB2和MySQL的值?抱歉,之前应该添加这一点。我现在更新了它。基本上,我从mysql中提取'start_date'值,它是一个日期数据type@GordonLinoffmysql查询中的其他$row值比较不错,但它根本不允许我以这种方式比较日期值。你在哪里做比较。该问题应与该应用程序一起标记。或者,您应该显示完整(或简化)查询,显示如何将来自两个不同数据库的数据组合在一起。我现在明白您的意思了。我已经更新了脚本部分,感谢您的深思熟虑的回答,我认为这是有意义的。那么,如果我去掉这样的破折号,我会得到一个准确的比较吗?i、 e.如果我的开始日期值是2018-02-02,它变成了20180202,那么DB2日期在几天和几个月内会比较准确吗?另外,我刚刚尝试过这个方法,但是如果我按原样做,使用双引号,它会失败,并说:“{”对于我的行值是意外的。如果我将该行更改为使用单引号,则它表示“-”的使用无效删除
{
}”
。DB2日期不是日期,而是一个表示日期的压缩数字。它不知道年、月和日。但是,如果您确保它的格式为YYYYMMDD,那么它将正确处理比较。问题是,当这些数字日期的格式为MMDDYYYY或DDMMYYYY时,您在编译时会遇到问题与
=
以外的任何内容一起使用。这现在非常有意义,是的,它们的格式肯定是YYYYMMDD。谢谢,我现在可以使用了!