postgresql 通过 PHP 从 Postgres 正确读取布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1314489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading boolean correctly from Postgres by PHP
提问by Léo Léopold Hertz ??
The main problem of this thread is moved to hereabout boolean datatype in PHP / Postgres.
该线程的主要问题移至此处关于 PHP / Postgres 中的布尔数据类型。
The problem is the conversion of t
and f
to true
and false
, since Postgres stores true
and false
as such.
问题是and到and的转换,因为 Postgres 存储等等。t
f
true
false
true
false
How can you use the variable a_moderator
in SESSION?
如何a_moderator
在 SESSION 中使用变量?
I fetch the value of the variable a_moderator
by
我取变量的值a_moderator
由
#1 code of how I get the variable
#1 获取变量的代码
$result = pg_prepare($dbconn, "moderator_check_query",
"SELECT a_moderator
FROM users
WHERE email = ;"
);
$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'];
}
I use it unsuccessfully by
我使用它失败
#2 code of how I use the variable unsuccessfully
#2 我如何使用变量失败的代码
if ( $_SESSION['login']['a_moderator'] == 't' ) {
// do this
}
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
我也没有成功地运行值,例如true
在t
. 在会话中变量的值为f
使得
#3 Output which tells me he value of the varibale
#3 输出告诉我变量的值
Array ( [login] => Array ( [passhash_md5] => dd2f85814c35fd465c30b1472f5d3af8 [email] => [email protected] [logged_in] => 1 [user_id] => 13 [username] => oeauoeh [a_moderator] => t ) )
Array ( [login] => Array ( [passhash_md5] => dd2f85814c35fd465c30b1472f5d3af8 [email] => [email protected] [logged_in] => 1 [user_id] => 13 [username] => oeauoeh [a_moderator] => t ) )
回答by marko
Select boolean field from postgre as ::int and in php cast to bool.
从 postgre 中选择布尔字段为 ::int 并在 php 中选择为 bool。
"SELECT a_moderator::int
FROM users
WHERE email = ;"
$isModerator = (bool)$row['a_moderator'];
$isModerator = (bool)$row['a_moderator'];
回答by Frank Farmer
This isn't a direct answer to the question, but here's an example demonstrating that pg_*() functions do in fact return the postgres boolean true value as the PHP string 't':
这不是对问题的直接回答,但这里有一个示例,演示 pg_*() 函数实际上确实将 postgres 布尔真值作为 PHP 字符串 't' 返回:
[example]$ cat scratch.php
<?php
//connect to the database...
require_once 'db_connect.php';
//query
$rows = pg_fetch_all(pg_query('SELECT TRUE::bool AS true'));
//dump returned array, and test with type-safe identity comparator
var_dump($rows, $rows[0]['true'] === 't');
[example]$ php scratch.php
array(1) {
[0]=>
array(1) {
["true"]=>
string(1) "t"
}
}
bool(true)
[example]$
回答by S. Cultura
I wrote a function in postgres to export boolean to PHP readable boolean:
我在 postgres 中编写了一个函数来将布尔值导出为 PHP 可读的布尔值:
You just use it this way:
你只需这样使用它:
SELECT php_bool(columna) from
table
Here is the function:
这是函数:
CREATE OR REPLACE FUNCTION php_bool(boolean)
RETURNS numeric LANGUAGE plpgsql
AS
$BODY$
BEGIN
IF = true THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END
$BODY$
回答by Rob Drimmie
Try:
尝试:
if ( $_SESSION['login']['a_moderator'] ) {
// do this
}
It is a boolean, not a string.
它是一个布尔值,而不是一个字符串。