use strict;
use warnings;
use Test::More tests => 10;
use DBI;
my $dbuser = shift @ARGV || 'foo';
my $dbpass = shift @ARGV || 'bar';
my $val = 1679971;
my $dbh = DBI->connect ('dbi:mysql:test', $dbuser, $dbpass)
or die "Conn error: $DBI::errstr";
ok ($dbh->do('CREATE TABLE foo ( bar FLOAT )'), 'Table created');
try_now ($dbh, $val);
$dbh->do ('DROP TABLE foo');
ok ($dbh->do('CREATE TABLE foo ( bar FLOAT (10,0) )'), 'Table created
+with right precision');
try_now ($dbh, $val);
$dbh->do ('DROP TABLE foo');
$dbh->disconnect;
sub try_now {
my ($dbh, $val) = @_;
my $out = 0;
ok ($dbh->do('INSERT INTO foo VALUE (?)', undef, $val), 'Value
+ inserted');
ok (my $sth = $dbh->prepare ('SELECT bar FROM foo'), 'Select p
+repared');
ok ($sth->execute, 'Select executed');
($out) = $sth->fetchrow_array;
is ($val, $out, "Output value $out equals input value $val");
}
Here's my result of running this:
1..10
ok 1 - Table created
ok 2 - Value inserted
ok 3 - Select prepared
ok 4 - Select executed
not ok 5 - Output value 1679970 equals input value 1679971
# Failed test 'Output value 1679970 equals input value 1679971'
# at myfloat.t line 30.
# got: '1679971'
# expected: '1679970'
ok 6 - Table created with right precision
ok 7 - Value inserted
ok 8 - Select prepared
ok 9 - Select executed
ok 10 - Output value 1679971 equals input value 1679971
# Looks like you failed 1 test of 10.
So, it's nothing to do with Perl and shows that your PHP script is telling porkies.
Update: I suggest you read all about FLOAT in MySQL and particularly the caveats about rounding, precision and exact comparison. |