PHP ile Postgres doğru şekilde boolean Okuma

4 Cevap php

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

Sorun, conversion of t ve f true ve false, Postgres saklanması true ve {[(olduğunu 3)]} gibi.


How can you use the variable a_moderator OTURUM?

Ben değişkenin değerini a_moderator tarafından getir

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

Ben tarafından başarısız kullanabilirsiniz

#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] => nthoaeuntht@Thnatuh.comn 
   [logged_in] => 1 [user_id] => 13 
   [username] => oeauoeh 
   [a_moderator] => t ) 
)

4 Cevap

:: Bool int ve php döküm gibi Postgre'nin gelen mantıksal bir alan seçin.

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

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

Bu soruya doğrudan bir cevap değil, ama burada bu pg_ * () işlevleri aslında postgres PHP dizge 't' olarak gerçek değerini boolean iade edebilirim gösteren bir örnek:

[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]$

Ben PHP okunabilir mantıksala boolean ihracat postgres'in bir işlevi yazdı:

Sadece bu şekilde kullanın:

SELECT php_bool(columna) from
table

İşte fonksiyonudur:

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$

Deneyin:

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

Bu bir boolean değil, bir dizedir.