通过PHP从Postgres正确读取布尔值 [英] Reading boolean correctly from Postgres by PHP
本文介绍了通过PHP从Postgres正确读取布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题是 t
和<$的转换c $ c> f 到 true
和 false
,因为Postgres存储 true
和 false
这样。
The problem is the conversion of t
and f
to true
and false
, since Postgres stores true
and false
as such.
如何在SESSION中使用变量 a_moderator
?
How can you use the variable a_moderator
in SESSION?
我通过
$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'];
}
我未成功使用
if ( $_SESSION['login']['a_moderator'] == 't' ) {
// do this
}
我也没有成功地将值 true
替换为 t
。
SESSION中的变量的值为 f
,这样
I also ran unsuccessufully the values such as true
in the place of t
.
The variable in the SESSION has the value f
such that
Array ( [login] => Array (
[passhash_md5] => dd2f85814c35fd465c30b1472f5d3af8
[email] => nthoaeuntht@Thnatuh.comn
[logged_in] => 1 [user_id] => 13
[username] => oeauoeh
[a_moderator] => t )
)
推荐答案
从postgre中将布尔字段选择为:: int并在php中将其强制转换为布尔值。
Select boolean field from postgre as ::int and in php cast to bool.
"SELECT a_moderator::int
FROM users
WHERE email = $1;"
$ isModerator =(bool)$ row ['a_moderator'];
$isModerator = (bool)$row['a_moderator'];
这篇关于通过PHP从Postgres正确读取布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文