php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45152 Boolean variables returned by pg_fetch_array
Submitted: 2008-06-02 21:19 UTC Modified: 2008-07-26 01:08 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:0 (0.0%)
From: ernesto_sanz at hotmail dot com Assigned:
Status: Not a bug Package: PostgreSQL related
PHP Version: 5.2.6 OS: Ubuntu 7.04
Private report: No CVE-ID: None
 [2008-06-02 21:19 UTC] ernesto_sanz at hotmail dot com
Description:
------------
I'm using Ubuntu Feisty Fawn 7.04 with Apache with PHP 5.2.4 and Postgres v.8.2.4.

I have noticed that, when querys are run, logical variables are not correctly assigned.
E.g. being 'lists' the table defined by the SQL sentence:

CREATE TABLE lists
(
  active boolean NOT NULL DEFAULT true,
  reference serial NOT NULL,
  caducated boolean NOT NULL DEFAULT false,
  name character varying(100) NOT NULL,
  ...
)

and the PHP code 
// creation of the SQL sentence
   $consulta="SELECT * FROM lists WHERE ((referencie=".$mykey.") AND     
              (active=true))";
// Execution 
$result = pg_query($consulta) 
          or die('Error in SQL query:'.pg_last_error());

if (pg_num_rows($resultado)>0) {		{ 
	$line = pg_fetch_array($result, null, PGSQL_ASSOC);
	if ($line["caducated"]==true) 
		{  ... -code executed if condition=true-  }
	else
		{  ... -code executed if condition=false-  }
   ...

I realized that the 'else' code was executed ALWAYS. It did not matter if the condition
was $line["caducated"]==True, or TRUE, or 1, or False or FALSE or 0.

I noticed that logical variables are assigned to 't' or 'f' instead of 'true' or 'false' (or numbers)
(although their type -boolean- is correct- and, that the only way to execute correctly the 'if' sentence
is by typing "if ($line["caducated"]==t)" (or "$line["caducated"]==f") which violates the reference syntax
of PHP, that states that to specify a boolean literal we have to use keywords TRUE or FALSE (both case-insensitive).




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-03 23:24 UTC] ernesto_sanz at hotmail dot com
Description:
------------
I'm using Ubuntu Feisty Fawn 7.04 with Apache with PHP 5.2.4 and
Postgres v.8.2.4.

I have noticed that, when querys are run, logical variables are not
correctly assigned.
E.g. being 'lists' the table defined by the SQL sentence:

CREATE TABLE lists
(
  active boolean NOT NULL DEFAULT true,
  reference serial NOT NULL,
  caducated boolean NOT NULL DEFAULT false,
  name character varying(100) NOT NULL,
  ...
)

and the PHP code 
// creation of the SQL sentence
   $consulta="SELECT * FROM lists WHERE ((referencie=".$mykey.") AND    

              (active=true))";
// Execution 
$result = pg_query($consulta) 
          or die('Error in SQL query:'.pg_last_error());

if (pg_num_rows($resultado)>0) {		{ 
	$line = pg_fetch_array($result, null, PGSQL_ASSOC);
	if ($line["caducated"]==true) 
		{  ... -code executed if condition=true-  }
	else
		{  ... -code executed if condition=false-  }
   ...

I realized that the 'else' code was executed ALWAYS. It did not matter
if the condition was $line["caducated"]==True, or TRUE, or 1, or False or FALSE or 0.

I noticed that logical variables are assigned to 't' or 'f' instead of
'true' or 'false' (or numbers) and their type is not correct (the result of gettype($line["caducated"]) is "string"- , so, the only way to execute correctly the 'if' sentence is by typing "if ($line["caducated"]=='t')" (or "$line["caducated"]=='f'")
which violates the reference syntax of PHP, that states that to specify a boolean literal we have to use keywords TRUE or FALSE (both case-insensitive).


Also, sometimes, asignation of variables inside the if or else blocks is made in a wrong way.
 [2008-07-26 01:08 UTC] hholzgra@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

pg_fetch_*() returns boolean values in the same way as psql displays them in query results: as the strings 't' for true and 'f' for false.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC