Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading boolean correctly from Postgres by PHP

Tags:

php

postgresql

The main problem of this thread is moved to here about boolean datatype in PHP / Postgres.

The problem is the conversion of t and f to true and false, since Postgres stores true and false as such.


How can you use the variable a_moderator in SESSION?

I fetch the value of the variable a_moderator by

#1 code of how I get the variable

    $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'];
    } 

I use it unsuccessfully by

#2 code of how I use the variable unsuccessfully

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

#3 Output which tells me he value of the varibale

Array ( [login] => Array ( 
   [passhash_md5] => dd2f85814c35fd465c30b1472f5d3af8 
   [email] => [email protected] 
   [logged_in] => 1 [user_id] => 13 
   [username] => oeauoeh 
   [a_moderator] => t ) 
)
like image 471
Léo Léopold Hertz 준영 Avatar asked Aug 21 '09 22:08

Léo Léopold Hertz 준영


People also ask

How check boolean is true in PHP?

The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

Is boolean supported in PHP?

PHP uses the bool type to represent boolean values. To represent boolean literals, you can use the true and false keywords. These keywords are case-insensitive.

Does PostgreSQL have boolean?

PostgreSQL provides the standard SQL type boolean ; see Table 8.19. The boolean type can have several states: “true”, “false”, and a third state, “unknown”, which is represented by the SQL null value.

How does Postgres store boolean?

PostgreSQL supports a single Boolean data type: BOOLEAN that can have three values: true , false and NULL . PostgreSQL uses one byte for storing a boolean value in the database. The BOOLEAN can be abbreviated as BOOL . In standard SQL, a Boolean value can be TRUE , FALSE , or NULL .


3 Answers

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'];

like image 187
marko Avatar answered Oct 20 '22 09:10

marko


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':

[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]$
like image 21
Frank Farmer Avatar answered Oct 20 '22 09:10

Frank Farmer


I wrote a function in postgres to export boolean to PHP readable boolean:

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 $1 = true THEN
    RETURN 1;
  ELSE
    RETURN 0;
  END IF;
END
$BODY$
like image 1
S. Cultura Avatar answered Oct 20 '22 08:10

S. Cultura