469,642 Members | 1,261 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

MySql VCHAR column converted to float by PHP :-(

Hi all
I fetch the following value from a string (VCAHR(250))colmun in a MySql
table:
"3000000000000000000000000000000000000000000000000 00"

When I write it out using echo() I get :
3E+50

Appearently the PHP interpreter converts the VCAHR value automatically
in a float despite the fact that the value is defined as a VCAHR in
MySql.
While trying to convert float to string back in PHP using type casting
or settype() I get always the string "3E+50".

Does someone know how one can fetch the VCHAR as string?
Does someone know how to print a formatted float to get the desired
result? I tried printf() but no success.

Thansk a lot:-)

Ramon

Mar 1 '06 #1
4 2918
ra*******@googlemail.com wrote:
Hi all
I fetch the following value from a string (VCAHR(250))colmun in a MySql
table:
"3000000000000000000000000000000000000000000000000 00" When I write it out using echo() I get :
3E+50 Appearently the PHP interpreter converts the VCAHR value automatically
in a float despite the fact that the value is defined as a VCAHR in
MySql.
While trying to convert float to string back in PHP using type casting
or settype() I get always the string "3E+50". Does someone know how one can fetch the VCHAR as string?
Does someone know how to print a formatted float to get the desired
result? I tried printf() but no success. Thansk a lot:-) Ramon


try

$variable =strval($data); // get string value of a number

http://us2.php.net/manual/en/function.strval.php

Mar 1 '06 #2
Unfortunetly it does not help :-(

Mar 1 '06 #3
On 1 Mar 2006 08:24:40 -0800, ra*******@googlemail.com wrote:
Appearently the PHP interpreter converts the VCAHR value automatically
in a float despite the fact that the value is defined as a VCAHR in
MySql.


PHP does not automatically convert any values unless there is a need.
You must have something in your code forcing it to a float. Do a
var_dump() right when you retrieve it from MySQL.

You might want to post some code as to how you are getting the result
from MySQL.

Mar 1 '06 #4
On 1 Mar 2006 08:24:40 -0800, ra*******@googlemail.com wrote:
Hi all
I fetch the following value from a string (VCAHR(250))colmun in a MySql
table:
"3000000000000000000000000000000000000000000000000 00"

When I write it out using echo() I get :
3E+50

Appearently the PHP interpreter converts the VCAHR value automatically
in a float despite the fact that the value is defined as a VCAHR in
MySql.
While trying to convert float to string back in PHP using type casting
or settype() I get always the string "3E+50".

Does someone know how one can fetch the VCHAR as string?
Does someone know how to print a formatted float to get the desired
result? I tried printf() but no success.


You're going to have to post a minimal example demonstrating it, along with
your PHP and MySQL version because on my system it shows the full string as
expected:

andyh@server ~ $ mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40 to server version: 4.1.7

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> connect test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

mysql> drop table t;
Query OK, 0 rows affected (0.03 sec)

mysql> create table t (c varchar(250));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into t values
('300000000000000000000000000000000000000000000000 000');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t;
+-----------------------------------------------------+
| c |
+-----------------------------------------------------+
| 30000000000000000000000000000000000000000000000000 0 |
+-----------------------------------------------------+
1 row in set (0.01 sec)

mysql> exit
Bye

andyh@server ~ $ cd public_html/
andyh@server ~/public_html $ cat test.php
<?php
$conn = mysql_connect();
mysql_select_db('test');
$res = mysql_query('select * from t');
$row = mysql_fetch_assoc($res);
var_dump($row);
print "\n";
print $row['c'];
print "\n";
?>
andyh@server ~/public_html $ php -q test.php
array(1) {
["c"]=>
string(51) "3000000000000000000000000000000000000000000000000 00"
}

30000000000000000000000000000000000000000000000000 0

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Mar 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nik Coughin | last post: by
4 posts views Thread by Mick White | last post: by
3 posts views Thread by Marcus | last post: by
1 post views Thread by luc h | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.