php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64037 Firebird return wrong value for numeric field
Submitted: 2013-01-21 13:18 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: vberko at mail dot com Assigned:
Status: Closed Package: PDO related
PHP Version: 5.4.11 OS: Windows
Private report: No CVE-ID:
 [2013-01-21 13:18 UTC] vberko at mail dot com
Description:
------------
When i store -1 value in a numeric field and i read back, i get -0.00


Test script:
---------------
SET SQL DIALECT 3;
SET NAMES WIN1250;
CREATE DATABASE '127.0.0.1/gds_db:C:\TEST\test2.fdb' USER 'test' PASSWORD 'test' PAGE_SIZE = 4096 DEFAULT CHARACTER SET WIN1250;
CREATE TABLE PRICE (ID INTEGER NOT NULL, TEXT VARCHAR(10), COST NUMERIC(15, 2));
INSERT INTO PRICE (ID, TEXT, COST) VALUES (2, 'test', -1);

$db=new PDO("firebird:dbname=localhost:c:/TEST/test2.fdb","test","test",array());
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);			
$sql="select id,cost from price where id=2";
$q=$db->query($sql);
$ret=$q->fetchAll();
var_dump($ret);

result:
array(1) { [0]=> array(4) { ["ID"]=> string(1) "2" [0]=> string(1) "2" ["COST"]=> string(5) "-0.00" [1]=> string(5) "-0.00" } } 

Expected result:
----------------
I get back -0.00 but the right result is -1.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-21 19:39 UTC] matheus at gigatron dot com dot br
This still happens on php5.5 beta1. 

Also note that any value other than "-1" (0.99, -1.01, etc) will **not** trigger the error.
 [2013-03-21 20:12 UTC] matheus at gigatron dot com dot br
Figured out the problem. It's a one character fix on firebird_statement.c, around line 347. 

The case when (n < f) must be (n <= f).

			if (n >= 0) {
				*len = slprintf(*ptr, CHAR_BUF_LEN, "%" LL_MASK "d.%0*" LL_MASK "d", 
					n / f, -var->sqlscale, n % f);
			} else if (n <= -f) {
				*len = slprintf(*ptr, CHAR_BUF_LEN, "%" LL_MASK "d.%0*" LL_MASK "d",
					n / f, -var->sqlscale, -n % f);				
			 } else {
				*len = slprintf(*ptr, CHAR_BUF_LEN, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
			}

If needed by the developers, I'll try and make a patch available.
 [2013-05-30 16:32 UTC] slavb18 at gmail dot com
may be related problem, but is not fixed by patch "around line 347"

/* procedure text

create or alter procedure TESTPROC
returns (
    RES numeric(15,2))
as
begin
  /* Procedure Text */
  res=15123.23;
  suspend;
end
*/

$q="Select * from testproc";
$sth=$pdo->query($q);
$row=$sth->fetch(PDO::FETCH_ASSOC);
print_r($row);

Outputs random number, like:

Array
(
    [RES] => 1396457606527.04
)


istead of

Array
(
    [RES] => 15123.23
)
 [2013-05-31 14:33 UTC] mbeccati@php.net
Automatic comment on behalf of matheus@gigatron.com.br
Revision: http://git.php.net/?p=php-src.git;a=commit;h=65d233f06c6e274f9559880a7c187b35932b1918
Log: Fixed bug #64037 (wrong value returned when using a negative numeric field equal to the scale)
 [2013-05-31 14:33 UTC] mbeccati@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 08:02:33 2014 UTC