为什么pg_query_params读取布尔值作为字符串?

时间:2022-06-12 15:22:52

I have a boolean column in a PostgreSQL table. In pgAdmin III, the table view shows 'TRUE' and 'FALSE' for the column. When I do a SQL 'select *', that other window shows 't' and 'f'. Is this some configuration problem? Shouldn't it also show 'TRUE' and 'FALSE'?

我在PostgreSQL表中有一个布尔列。在pgAdmin III中,表视图显示列的“TRUE”和“FALSE”。当我执行SQL'select *'时,其他窗口显示't'和'f'。这有些配置问题吗?它不应该也显示'TRUE'和'FALSE'吗?

Then we come to PHP. I have the following code to deal with this boolean (actually I'm doing something different, but simplified it here):

然后我们来PHP。我有以下代码来处理这个布尔值(实际上我正在做一些不同的东西,但在这里简化它):

$q = "select * from tax where id = $1";
$res = pg_query_params($conn,$q,[$id]);
if ($res) {
    $row = pg_fetch_array($res,NULL,PGSQL_ASSOC);
    $valido = $row['valido'];
    echo $valido; // prints 'f' (== 'FALSE' on database)
    if ($valido) {
        echo 'valid'; // gets here
    } else {
        echo 'invalid';
    }
}

The database says 'FALSE', php reads 'f' and treat it as 'TRUE'. Is this some faulty configuration, or php always reads a boolean value as a text interpreted as the opposite of the original value?

数据库显示'FALSE',php读取'f'并将其视为'TRUE'。这是一些错误的配置,或者php总是读取一个布尔值作为解释为与原始值相反的文本?

1 个解决方案

#1


1  

Let's face it:

面对现实吧:

  • Data returned by either PDO or the pgsql libraries are always strings.
  • PDO或pgsql库返回的数据始终是字符串。
  • Postgresql representation for booleans are strings 't' and 'f'.
  • booleans的Postgresql表示是字符串't'和'f'。

This is specified by the PHP documentation in the "returned values" of the different fetch functions. here and here.

这由PHP文档在不同的提取函数的“返回值”中指定。在这里和这里。

If you want these results to be cast to PHP equivalent types, you must set up a converter system.

如果希望将这些结果转换为PHP等效类型,则必须设置转换器系统。

#1


1  

Let's face it:

面对现实吧:

  • Data returned by either PDO or the pgsql libraries are always strings.
  • PDO或pgsql库返回的数据始终是字符串。
  • Postgresql representation for booleans are strings 't' and 'f'.
  • booleans的Postgresql表示是字符串't'和'f'。

This is specified by the PHP documentation in the "returned values" of the different fetch functions. here and here.

这由PHP文档在不同的提取函数的“返回值”中指定。在这里和这里。

If you want these results to be cast to PHP equivalent types, you must set up a converter system.

如果希望将这些结果转换为PHP等效类型,则必须设置转换器系统。