Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

php mysql statement not working on decimal value


Go to solution Solved by Perplexity 🤖,

Recommended Posts

I have a query that only works using like but not with =  on decimal values even if the values are exact: 

$sql_amount=mysqli_query($con,"update $table set item='$item1' where cost= 27.55" );
$sql_amount=mysqli_query($con,"update $table set item='$item1' where cost like 27.55 " );

The second way works but I can't figure out why the first way doesn't. 

I can reverse the first query using set cost = 27.55 where item='$item1'  & it works fine.

Is there something I'm missing in the first command that will not let me use the "where" with an = sign on a decimal?

  • Solution

you should use a decimal data type.

a float data type suffers from floating point conversion errors, because some values cannot be exactly stored.

the = comparison doesn't work because the actual value as a float is something like 27.550001 (just an example)

the like comparison works because the value is converted to a string and the like compares the values character by character.

Yes, float is not the right data type to use for storing monetary values.  Use a decimal.  Here's an example:

 

CREATE TABLE Product (
  ProductID INT AUTO_INCREMENT KEY,
  item varchar(20),
  cost decimal(6,2)
);

INSERT INTO Product (item, cost) values ('shoe', 27.55);
INSERT INTO Product (item, cost) values ('hat', 27.89);

UPDATE Product set item = 'sneaker' where cost = 27.55;

You can see this example in action at db-fiddle:  https://www.db-fiddle.com/f/veSL2xerpodFq79N2e4rUi/0

As an editorial comment, why are you interpolating the name of the table from a variable into a string?  Use the table name.  Your queries should also be using bind variables.

As usuall you guys are right on the money. 

I spent all day working on this & even spent about an hour google searching to no avail.

Why i never thought that float could be a problem was because the number sits right in the database as is & prints out as it is also.

It's only the the = sign that doesn't recognize it.  

  On 3/28/2025 at 1:50 AM, gizmola said:

Yes, float is not the right data type to use for storing monetary values.  Use a decimal.  Here's an example:

 

CREATE TABLE Product (
  ProductID INT AUTO_INCREMENT KEY,
  item varchar(20),
  cost decimal(6,2)
);

INSERT INTO Product (item, cost) values ('shoe', 27.55);
INSERT INTO Product (item, cost) values ('hat', 27.89);

UPDATE Product set item = 'sneaker' where cost = 27.55;

You can see this example in action at db-fiddle:  https://www.db-fiddle.com/f/veSL2xerpodFq79N2e4rUi/0

 

Expand  

Can I assume if I wanted to have the capability of a number as large as 125000.50 that I would need to set something like " cost decimal(10,2)" for that.

Using the right data type for the right job is just part of the learning curve for any relational database.  The mysql manual is not the easiest manual to read, but there is this general page:  https://dev.mysql.com/doc/refman/8.4/en/precision-math-numbers.html

 

  Quote

The DECIMAL data type is a fixed-point type and calculations are exact. In MySQL, the DECIMAL type has several synonyms: NUMERIC, DEC, FIXED. The integer types also are exact-value types.

The FLOAT and DOUBLE data types are floating-point types and calculations are approximate. In MySQL, types that are synonymous with FLOAT or DOUBLE are DOUBLE PRECISION and REAL.

Expand  

 

Yes, set your definition as required for the maximum size of the number you need to handle.  

The M precision can be as large as 64 digits, but you should always try and size it to maximum size you need.

If you truly need 10 digits to the left of the decimal point, then you should define the number as decimal(12, 2)

  On 3/28/2025 at 2:06 AM, garyed said:

Can I assume if I wanted to have the capability of a number as large as 125000.50 that I would need to set something like " cost decimal(10,2)" for that.

Expand  

To clarify, for DECIMAL(M, D) the M is maximum number of digits, less the reserved decimal places.  So with your example the max number would be:  99,999,999.99

What I would like to do is have most of the numbers go to 2 decimals & just a few go to 4 decimals. 

The problem with that is if I set decimals to (10,4) so it will read a number like 32.0756 then 27.55 goes to 27.5500 & it looks kind of cumbersome. 

That's why I originally thought float was best for my database but I didn't know how floats were treated.  

  On 3/28/2025 at 2:52 AM, garyed said:

The problem with that is if I set decimals to (10,4) so it will read a number like 32.0756 then 27.55 goes to 27.5500 & it looks kind of cumbersome. 

Expand  

It sounds like you require 4 decimal places.  

There are a few tricks to handle suppression of trailing zeros.  They involve getting MySQL to manipulate the value by forcing it to cast datatypes. 

With MySQL 5.x

SELECT (cost + 0e0) cost FROM table

If you are using MariaDB or MySQL 8+ you can do this.

SELECT CAST(cost AS DOUBLE) cost FROM table
  On 3/29/2025 at 6:32 PM, gizmola said:

It sounds like you require 4 decimal places.  

There are a few tricks to handle suppression of trailing zeros.  They involve getting MySQL to manipulate the value by forcing it to cast datatypes. 

With MySQL 5.x

SELECT (cost + 0e0) cost FROM table

If you are using MariaDB or MySQL 8+ you can do this.

SELECT CAST(cost AS DOUBLE) cost FROM table
Expand  

Thanks, that's good info . 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.