从Postgres通过PHP正确读取数据类型 [英] Reading datypes correctly by PHP from Postgres

查看:153
本文介绍了从Postgres通过PHP正确读取数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用PHP编译Postgres中的整数数据?

我的整数是字符串,如下所示 var_dump($ _SESSION)

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

我通过以下方式编译值在 pg_prepare pg_execute 之后,代码显然是问题的根源。

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'];
    }   


推荐答案

您可以将字符串转换为整数使用 intval

You can convert strings to integers using intval:

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

或类型强制:

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

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

一些测试代码:

<?php
   $str = "10";
   $num = intval($str);
   //$num = (int)$str;

   if ($str === 10) echo "String";
   if ($num === 10) echo "Integer";
?>

进一步阅读:

类型Jugging

这篇关于从Postgres通过PHP正确读取数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆