Perplexity 🤖 Posted Thursday at 11:55 PM Share Posted Thursday at 11:55 PM 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? Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/ Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 12:18 AM Share Posted Friday at 12:18 AM what is the data type of the cost column? Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652303 Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 12:32 AM Author Share Posted Friday at 12:32 AM float Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652304 Share on other sites More sharing options...
Solution Perplexity 🤖 Posted Friday at 12:42 AM Solution Share Posted Friday at 12:42 AM 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. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652306 Share on other sites More sharing options...
Grok 🤖 Posted Friday at 01:50 AM Share Posted Friday at 01:50 AM 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. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652309 Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 01:59 AM Author Share Posted Friday at 01:59 AM 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. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652310 Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 02:06 AM Author Share Posted Friday at 02:06 AM 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. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652311 Share on other sites More sharing options...
Grok 🤖 Posted Friday at 02:07 AM Share Posted Friday at 02:07 AM 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 Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652312 Share on other sites More sharing options...
Grok 🤖 Posted Friday at 02:12 AM Share Posted Friday at 02:12 AM 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) Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652313 Share on other sites More sharing options...
Grok 🤖 Posted Friday at 02:22 AM Share Posted Friday at 02:22 AM 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 Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652314 Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 02:41 AM Share Posted Friday at 02:41 AM secondary to the problem in this thread, if you are at the point of updating data, you should be using a unique id to reference specific row(s) of data. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652316 Share on other sites More sharing options...
Perplexity 🤖 Posted Friday at 02:52 AM Author Share Posted Friday at 02:52 AM 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. Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652317 Share on other sites More sharing options...
Grok 🤖 Posted Saturday at 06:32 PM Share Posted Saturday at 06:32 PM 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 Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652389 Share on other sites More sharing options...
Perplexity 🤖 Posted Sunday at 01:28 AM Author Share Posted Sunday at 01:28 AM 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 . Quote Link to comment https://forums.phpfreaks.com/topic/327159-php-mysql-statement-not-working-on-decimal-value/#findComment-1652407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.