php - 通过 PHP 从 Postgres 正确读取 bool 值

标签 php postgresql

本帖主要问题移至here关于 PHP/Postgres 中的 bool 数据类型。

问题是 tftruefalse 的转换,因为 Postgres存储 truefalse


如何在 SESSION 中使用变量 a_moderator

我通过

获取变量a_moderator的值

#1 如何获取变量的代码

    $result = pg_prepare($dbconn, "moderator_check_query", 
        "SELECT a_moderator 
        FROM users
        WHERE email = $1;"
    );
    $a_moderator = pg_execute($dbconn, "moderator_check_query", array($_SESSION['login']['email']));

    $rows = pg_fetch_all ( $a_moderator );
  
    foreach ( $rows as $row ) {
       $_SESSION['login']['a_moderator'] = $row['a_moderator'];
    } 

我使用它不成功

#2 我如何使用变量失败的代码

if ( $_SESSION['login']['a_moderator'] == 't' ) {
   // do this
}

我在 t 的地方也运行了诸如 true 之类的值,但没有成功。 SESSION 中的变量具有值 f 使得

#3 输出告诉我变量的值

Array ( [login] => Array ( 
   [passhash_md5] => dd2f85814c35fd465c30b1472f5d3af8 
   [email] => nthoaeuntht@Thnatuh.comn 
   [logged_in] => 1 [user_id] => 13 
   [username] => oeauoeh 
   [a_moderator] => t ) 
)

最佳答案

从 postgre 中选择 boolean 字段作为::int 并在 php 中转换为 bool。

 "SELECT a_moderator::int 
        FROM users
        WHERE email = $1;"

$isModerator = (bool)$row['a_moderator'];

关于php - 通过 PHP 从 Postgres 正确读取 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1314489/

相关文章:

php - Laravel:在多个链接中仅显示某些列值的数据

ruby-on-rails - 如何解决 Rails、GeoRuby(和 postgis/postgresql): NameError: uninitialized constant Point?

Django 1.2 PostgreSQL 使用 ON DELETE NO ACTION 级联删除键

java - 我可以使用 Spring Framework 分离 JDBC 读取和写入吗?

php - 从具有关系的表中检索帖子标签

javascript - 在下拉列表中选择后隐藏或删除弹出的颜色框上的复选框

php - 我如何判断 MySQL 中的日期时间字段是否在过去的 PHP 中?

PHP 扩展 : convert an object to string with __toString()

ruby-on-rails - Rails Autocomplete 在本地工作但在 Heroku 上不工作,可能是 MySQL/Postgre 问题

PostgreSQL 触发器 : check total number of rows in WHEN conditon