0

Here is my code:

DATABASE SETUP

<?php
$user="k*********";
$password="*********";
$database="**********";
mysql_connect("localhost",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "CREATE TABLE rn (
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
dait CHAR(10),
rnfl INT ,
)";
mysql_query($query);
mysql_close();
?>

database update

<?php
// Connect to MySQL
mysql_connect("localhost", "*********", "**************") or die(mysql_error());
mysql_select_db("********") or die(mysql_error());
$dait="31/7/2014";
$rnfl=12.9;
// Update  
$result = mysql_query("UPDATE rn SET dait='$dait', rnfl='$rnfl' WHERE id=1") 
or die(mysql_error());  
$result = mysql_query("SELECT * FROM rn WHERE id=1") 
or die(mysql_error());  
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row['dait']." - "; 
printf('(%5.2d mm)', $row['rnfl']); 
?>

THIS IS WHAT I GET AFTER RUNNING THE UPDATE:

31/7/2014 - ( 13 mm)

what I am looking for is: -

31/7/2014 - (12.9 mm)
3
  • 1
    Use %f instead of %d. Commented Aug 5, 2014 at 3:56
  • stop using deprecated mysql_* functions; use MySQLi or PDO instead. Also, when you debug, remove those error-suppressing @ Commented Aug 5, 2014 at 3:57
  • Warning: mysql_ function is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Commented Aug 5, 2014 at 3:57

2 Answers 2

2

Well, you're trying to store a float (12.9) in an INT type column - integer. Ints do not support decimal places. So basically your question boils down to "I'm bought a banana at the store. Why isn't it an apple?"

Change rnfl's type to float or something like decimal(10,2) if you want to get that .9 back again.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry Marc B your suggestion didn't work for me. Any other suggestions to try?
There's also the %f comment above. even if you do change the DB to be a float, you're still telling mysql to printf() it as an int (%d) instead of a float (%f).
1

You put the type of rnfl field as int - the data will be rounded when you insert them.

Just change the type to float or, if you're dealing with money, decimal(10,2).

2 Comments

Sorry rcmachado your suggestion didn't work. Any other suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.