When i scan a barcode in "form" it automatically inserts it in database but two first digits are missing (two first zeros) . So , for example if a barcode 0020166196800002 is scanned and inserted into database only number 20166196800002 is inserted without first two digits of "00". Please, help!
Databases do strip leading zeros if the column type is numeric and there is no zero padding configured.
For example, MySQL allows a column to be defined with "zerofill(x)", which results in every number being at least x digits long, and a stored zero will come up as x 0 characters instead of only one.
Related
I have to put the price of some items inside a mysql table. When creating the table I'm using DECIMAL(10,2) as I don't need more than 2 digits after the comma (for example: 123,45 would be accepted as an input but 123,456 would be rounded to 123,45 with PHP).
First question: using DECIMAL(10,2), how do I know how many numbers may be stored before the comma? I know it is not 10, as 10 is just the precision Mysql uses when doing math with those numbers: so where the length of the number itself is specified?
Second question: I'm using PHP to round user input to fit the data type (float with 2 numbers after the comma). How should I use mysqli->bind_param to insert those data? Which of these datatypes (from the documentation) accepted by bind_param should I use (and possibly: why)?
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
DECIMAL(10,2) means that 10 is the maximum number of digits to be used. The 2 specifies that there are two on the right side of the decimal. So this means there are 8 digits left on the left side. See the manual
As for the datatype, you should use double as this is for decimal numbers.
Integer has no decimal chars, so it will floor all numbers. String is for text and not numeric values. Blob is for a binary large object, EG: an image
I have two columns on my database, sickleave and vacationleave. It has to be a decimal but for some reason I only get the whole number. For example, I input 1.50, when I press update I get the value 2. I tried setting the column type to decimal but I still get the same result.
Here's my database schema:
From the mysql documentation:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
So in your case: hit change button there and in the Length/value column there should be: 11,2 with no parentheses or anything else when you hit save
In case you want to do it manually there's a discussion of it here: MySQL - How do I update the decimal column to allow more digits?
And here's the alter: ALTER TABLE YourTableName MODIFY COLUMN column_name DECIMAL(11,2);
Tried altering it manually using:
ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);
Thanks #user3647971
I have field where i store my grand total to a mysql filed as total and its type set as decimal(10,2)
When my php form adds up and updates the query for 112381348.40 (i.e 112 million)
but when i fetch or view it on phpMyadmin, the value shows as 99999999.99
from my data type set, it has to be 10 digits and 2 decimals where as it is maximum updating as 8 and 2.
Am i understanding wrong or what am i doing wrong.
I know i can increase it to decimal(13,2) and then see it.
But i would like to know the reason, why 10,2 is not happening?
The specification for decimal(m, n) takes two arguments. The first is the "precision" and the second is the "scale". More colloquially, the first is the number of digits in the number. The second is the number of digits to the right of the decimal point.
So, your specification of decimal(10, 2) can store numbers like: 12,345,678.90 -- i.e., up to 99,999,999.99. And that is the maximum number.
If you want to store numbers larger than that, then use a larger precision for the number. For your number, you need at least decimal(11, 2), although I would suggest a larger precision so you don't encounter this problem in the future.
In setting a float or decimal field, the first int value is the total character count. The second is how many decimal places. So:
//decimal(10,2)
12345678.90
//decimal(13,2)
12345678901.23
//decimal(7,3)
1234.567
See?
You have 11 digits. The first number is the total digits. Try:
DECIMAL(11, 2)
PHP runs a script that correctly calculates a value. When I echo that value out it comes out as 4.865472349
Then a simple update script is used to enter the value into my database.
$query = "UPDATE members
SET rating = $r
WHERE username = '$username'";
mysql_query($query);
When I do this, the value that is entered into the database is 5.
If I replace $r in the previous formula with 4.865472349 directly, it produces the same result.
Clearly this is because my SQL type was set to "integer"
But Im not sure what to change it to in order to fix this issue. Any help?
If you want the same precision as 4.865472349, you can use DECIMAL(10,9)
Reference
For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes.
Assuming you are using MySql
You probably should use DOUBLE. It has the same precision and range as floating point numbers in PHP (in most configurations).
To switch the type using PHPMyAdmin:
Click on the table
Click on structure
Click on Change (the pencil icon) beside rating
Under "Type" select the option "DOUBLE"
Hit Save
You can use float, double or decimal depending on the numbers you'll be storing. I would say float is good enough.
Edit the column type in the database to be able to hold more than just an integer.
ALTER TABLE members
CHANGE rating rating float(10,9);
I have within a form a textbox named PO_Number. The form submit by post to another page the textbox value.
In the second page I get $_POST['PO_Number'] and enter in MySQL.
MySQL field is varchar(15). As soon as the string of PO_Number starts with a letter or a number everything is OK.
The problem: sometimes the PO (Purchase Order) number start with 00 or 000 and it is stored with a comma before the 00
For example:
GH93737 - works
9087893 - works
0011132 - entered in database as ,0011132 (see the comma?)
The insert looks normal:
mysql_query("INSERT INTO table_name (PO_Number, ....) VALUES ('".$_POST['PO_Number']."',......)");
Many thanks for your suggestions and your help.
I'm wondering if this has something to do with your browser/server character encoding and how it's interpreting those specific numbers because all of those leading zeros and ones might be getting interpreted as a binary number?
Here's some brief info on that point:
A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters.
http://htmlpurifier.org/docs/enduser-utf8.html