本帖主要问题移至here关于 PHP/Postgres 中的 bool 数据类型。
问题是 t
和 f
到 true
和 false
的转换,因为 Postgres存储 true
和 false
。
如何在 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/