You are here: Re: [PHP] Problems with strings being handled like numbers « PHP « IT news, forums, messages
Re: [PHP] Problems with strings being handled like numbers

Posted by "Richard Lynch" on 09/15/05 02:25

On Tue, September 13, 2005 7:48 pm, J B wrote:
> On 9/13/05, Chris <listschris@leftbrained.org> wrote:
>>
>> Are you sure you're getting the number as you need it from the
>> query?
>>
>> PHP won't (though I guess there could be a bug) automatically
>> convert a
>> numerical string into it's numerical equivalent.
>>
>> If the code you posted produces this problem, then the only
>> possiblities
>> I see are that the mssql module is returning a number, or the
>> database
>> query is returning that particular string.
>
>
> (Sorry to Chris for getting this twice, I accidentally didn't reply to
> the
> list)
>
> First of all, thanks for the absurdly fast reply.
>
> I've been playing with it and it looks like mssql_fetch_row may be the
> problem. Right after this line:
>
> $line = mssql_fetch_row($result);
>
> ...I inserted this line:
>
> var_dump($line[0]);
>
> ...and this is what I got back:
>
> string(20) "3.67124100206018e+17"
>
> The actual contents of that cell should be "367124100206018209". So it
> looks
> like the contents of the cell have already been mangled before they're
> even
> assigned to the array variables (which would explain why my previous
> attempts at typecasting at assignment time weren't having any
> effect...it
> was already a string). Is there some way I can tell mssql_fetch_row()
> to
> leave the contents alone?
>
> By the way, if I do "SELECT cast(Number as char) from Voucher..." and
> leave
> the rest of it the same, I do get the whole number...but that seems
> kinda
> inelegant. Is this expected behaviour for mssql_fetch_row()?

Yes.

It's not actually mssql_fetch_row() that is changing the data, per se.

mssql_* and PHP "know" that the data is supposed to be "integer"

But the largest integer that PHP can store is 0xFFFFFFFF.

So when PHP tries to accept the "integer" from mssql_fetch_row, it's
got no room to store it as integer, and HAS to convert it to float.

You need to keep it char if you expect to work with integers that large.

There are mathematical packages in PHP that let you manipulate large
integers. Well, actually, you manipulate large strings that happen to
LOOK like integers, since they're not really integers in PHP if
they're bigger than 0xFFFFFFFF

One of those extensions is called BC_MATH.
I forget the other (newer, better) extension name.

But I'm sure you can find it in http://php.net/manual as fast as I can.

If you JUST want to display it, and not manipulate it in PHP, you can
ignore that bit about BC_MATH and the other extension.

You'll need to use the typecast to char in MSSQL no matter what, cuz
ain't no way PHP is gonna store an integer bigger than 0xFFFFFFFF as
an integer.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация