PDO ODBC为列值返回null,但odbc_conect不返回

For some weird reason, my PDO keeps returning NULL when it should return a string/integer. I've tried to use the odbc_conect and it works fine.

Context:

  • CentOS 7
  • PHP 7.1.13
  • unixODBC 2.3.1 (x86_64 . 11.el7)
  • ODCB Driver: SAP HANA ODBC v2.2.36.1512099132 64 bits

When it's happening? Until now, I detected few:

  • Function returns: SELECT id,GEO.ST_Asgeojson() FROM DEV.GEOMETRIES
  • Using CAST: SELECT id, CAST(GEO.ST_AsText() AS VARCHAR) FROM DEV.GEOMETRIES or SELECT CAST(1 AS INTEGER) AS "int" FROM DUMMY
  • Any blob column : SELECT id,GEO FROM DEV.GEOMETRIES
  • Date Types: SELECT insertdate FROM DEV.GEOMETRIES

I also tried to use PDOStatement::bindColumn to force data types but was unsuccessful

Where the code :

<?php
try{
    ini_set('memory_limit','256M'); //odbc_fetch_array is hungry
    // $sql = 'SELECT  NOW()  AS  "now"  FROM  DUMMY';
     $sql = "SELECT id,GEO.ST_Asgeojson(),GEO.ST_AsText(),insertdate  FROM DEV.geometries limit 1";
     //$sql = "SELECT *  FROM DEV.geometries limit 1";
    // $sql = "SELECT  CAST(GEO.ST_AsText() AS VARCHAR) from geometries";
    // $sql = "SELECT  CAST(10 AS VARCHAR) from DUMMY";
    // $sql = "SELECT  CAST(10 AS INTEGER) from DUMMY";

    echo "SQL: ",$sql,PHP_EOL;

    $DSN = "DRIVER=HANADB;UID=DEV;PWD=DEV@pwd;SERVERNODE=192.168.1.163:39013;DATABASENAME=SYSTEMDB";
    $options = [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_CASE =>\PDO::CASE_LOWER, //Compatibility with mysql case
        \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
        \PDO::ATTR_EMULATE_PREPARES, true //With or without 
    ];
    echo "############# Using PDO:ODBC:",PHP_EOL;
    $DB =  new \PDO("odbc:".$DSN,'DEV','DEV@pwd',$options);

    echo "Driver: ",$DB->getAttribute(\PDO::ATTR_CLIENT_VERSION),PHP_EOL;
        echo "Using Prepared Statement:",PHP_EOL;


    $stmt = $DB->prepare($sql);
    // bindColumn Also doesn't work
    //$stmt->bindColumn('id', $id, \PDO::PARAM_INT);
    //$stmt->bindColumn('geojson', $geometry, \PDO::PARAM_STR); 
    $stmt->execute();

    /*while ($row = $stmt->fetch(\PDO::FETCH_BOUND)) {
         var_dump($row);
         var_dump($id, $geometry);
     }*/


    //var_dump($ret,$stmt);
    var_export($stmt->fetchAll());

    $stmt->closeCursor();
    echo PHP_EOL,"Using Query:",PHP_EOL;
    $qR = $DB->query($sql);
    foreach($qR as $row){
        var_export($row);
    }
    echo PHP_EOL,PHP_EOL,"#############  Using odbc_*( ) Functions",PHP_EOL;
    $conn = odbc_connect($DSN,'DEV','DEV@pwd');
    $rs = odbc_exec($conn,$sql);
      while($row = odbc_fetch_array($rs)){
          var_export($row);
      } 
}catch(\Exception $e){  
    echo $e->getMessage(),PHP_EOL,$e->getTraceAsString();
}

Output:

############# Using PDO:ODBC:
Using Prepared Statement:
array (
  0 =>
  array (
    'id' => '363491',
    'geojson' => NULL,
    'geotext' => NULL,
    '__dateinsert' => NULL,
    'info' => NULL,
  ),
)
Using Query:
array (
  'id' => '363491',
  'geojson' => NULL,
  'geotext' => NULL,
  'insertdate' => NULL,
  'info' => NULL,
)

#############  Using odbc_*( ) Functions
array (
  'id' => '363491',
  'geojson' => '{"type": "Point", "coordinates": [16.352878, 48.225628]}',
  'geotext' => 'POINT (16.352878 48.225628)',
  'insertdate  ' => '2018-02-06 15:08:19.414000000',
  'info' => NULL,
)

I'm not sure if it's a Drive problem because it works with odbc_* functions, maybe it's a PDO BUG.

douhezhang8932
douhezhang8932 驱动器是ODBC,使用PDO_ODBC连接通用ODBC驱动程序。关于Unix,版本和更多信息的上下文在问题中描述。感谢您努力寻求帮助。
2 年多之前 回复
du0531
du0531 该驱动程序甚至没有列在PHP文档页面(php.net/manual/en/ref.pdo-odbc.php)中。它很可能是驱动程序的问题,但我确实没有使用ODBC的经验,所以我可能会谈论完全不同的东西。我不确定你使用的是什么类型的Unix,但如果它是Debian(或衍生产品),它可能就像安装php-odbc软件包一样简单。
2 年多之前 回复
duanlei2458
duanlei2458 我刚从同一个驱动器测试了另一个版本,我不知道是否有其他驱动程序连接到SAPHana
2 年多之前 回复
doupu1949
doupu1949 你试过另一个司机吗?
2 年多之前 回复
dongshenling6585
dongshenling6585 这很奇怪,我认为这适用于blob或“特殊”类型,但也适用于INT和CHARs,左侧显示PDO提取右侧的odbc_*结果。diffchecker.com/MsJQq1Z5
2 年多之前 回复
douxian9706
douxian9706 它看起来像我可能是一个错误,但没有一个ODBC服务器来测试。你试过其他一个驱动程序吗?此外,所有列类型或仅特定列类型是否会发生此错误?如果是这样,哪个?
2 年多之前 回复
dongpiaozhao6836
dongpiaozhao6836 这是来自PDOStatement::errorInfo的转储,我删除它谢谢。
2 年多之前 回复
douzong3599
douzong3599 输出中的数组(3){...}来自哪里?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐