dos71253 2013-08-31 02:17
浏览 169
已采纳

用于Microsoft SQL Server的PHP ODBC PDO驱动程序返回空数组或false

I have this code:

$dbh = new PDO('odbc:MSSQLServer', 'user', 'pass');

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sth = $dbh->prepare($sql);

$sth->execute();

$result = $sth->fetch(PDO::FETCH_ASSOC);        

var_dump($result);

If:

$sql = "select seri_cdser from tsica_alun_matr";

The output is:

array(1) {
    ["seri_cdser"]=>
    string(4) "3EMM"
}

But if:

$sql = "select * from tsica_alun_matr";

The output is:

SQLSTATE[22003]: Numeric value out of range: 0 [Microsoft][SQL Server Native Client 11.0]Numeric value out of range (SQLFetchScroll[0] at /usr/local/src/PDO_ODBC/odbc_stmt.c:372)

Sometimes an empty array is returned too, apparently the driver is instable.

The table tsica_alun_matr has 13 columns.

Someone knows if it is a bug with PDO driver for MS SQL Server?

  • 写回答

2条回答 默认 最新

  • dtihe8614 2013-09-01 22:05
    关注

    I found a comment in PHP docs:

    http://php.net/manual/pt_BR/book.pdo.php

    That says:

    1. The second error I had was:

      Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22003]: Numeric value out of range: 0 [Microsoft][SQL Native Client]Numeric value out of range (SQLFetchScroll[0] at ext\pdo_odbc\odbc_stmt.c:372)' in (YOUR_TRACE_HERE) <<< Another meaningless error "Numeric value out of range"...

    -> I was actually returning a date datatype (datetime or smalldatetime) "as is", that is, without converting it to varchar before including it in the result set... I don't know if PDO is responsible for converting it to a PHP datatype, but it doesn't. Convert it before it reaches PHP.

    So, as exist datetime and smalldate fields i have changed my PDO query as below, converting datetime and smalldatetime to varchar. This page helps to choose the best converstion format.

    $sth = $dbh->prepare("
        select
            alun_cdal,
            seri_cdser,
            CONVERT(VARCHAR(23), matp_dat_Mat, 121) as matp_dat_Mat,
            alun_nmal,
            alun_sexo,
            CONVERT(VARCHAR(23), alun_dnsc, 121) as alun_dnsc
        from 
            tsica_alun_matr");
    

    Now my code is working fine!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器