dongli8722 2009-08-22 02:50
浏览 71
已采纳

从Postgres通过PHP正确读取数据类型

How do you compile integer data in Postgres by PHP?

My integers are string as the following show by var_dump ( $_SESSION )

  'logged_in' => int 1
  'user_id' => string '9' (length=1)                 // should be int
  'a_moderator' => string '0' (length=1)             // should be int

I compile the values by the following code which obviously is the source of the problem after pg_prepare and pg_execute.

    while ( $row = pg_fetch_array( $result ) ) { 
        $user_id = $row['user_id'];
    }   
  • 写回答

2条回答 默认 最新

  • doubi7306 2009-08-22 02:52
    关注

    You can convert strings to integers using intval:

    $str = "5";
    $num = intval($str);
    

    or by type coercion:

    $str = "5";
    $num = (int) $str;
    
    //this works too
    $num = (integer) $str;
    

    some test code:

    <?php
       $str = "10";
       $num = intval($str);
       //$num = (int)$str;
    
       if ($str === 10) echo "String";
       if ($num === 10) echo "Integer";
    ?>
    

    Further reading:

    Type Jugging

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

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器