Mysql Changing My Float Values - php

I have a database that stores lat and long coords, along with other fields.
The lat and long are set to float 10,8.
When running the following command ...
INSERT IGNORE INTO records(unique_id, city, st,zip,lat,lon) VALUES
( '80936EN476', 'West Jordan', 'UT', '84088-5205', '40.59660', '-111.963' )
The insert is completed with no messages, but when the record is retrieved, the "lon" field has been set to -100.000000.
When I try to edit the value in phpMyAdmin, I get the following error.
Warning: #1264 Out of range value for column 'lon' at row 1

You need to put more digits in front.
10,8 means 2 digits before decimal and 8 decimal digits.
try putting something like 12,8

Do not use (m,n) on FLOAT or DOUBLE. It leads to an extra rounding. And, in your case, it leads to truncation in the upper digits.

Ok, the issue turned out to be the float values.
I changed it to 10,6 and that resolved the issue!

Related

MySQl leading zeros get stored but wrong display

I am trying many typed of fields in MySQL to store numbers only, tried with INT, BIGINT with leading zeros, CHAR and VARCHAR to store INVOICE NUMBERS
I need the invoice numbers to be start with 0000000001, I stored it manually in PHPmyadmin
Now I want to display it and I dont get the leading zeros ....
Here is the database
field "folio" CHAR 15 stored I have manually did 0000000001 it displays fine on phpmyadmin
but here is the problem
<?php $maxprod=mysqli_query($datacenter,
"SELECT * FROM ventas
WHERE documento = 'boleta'
ORDER BY id DESC LIMIT 1");
while($lastcode=mysqli_fetch_assoc($maxprod)){?>
<input type="text" value="<?php echo $lastcode['folio']+1?>">
<?php }?>
the result of the query is 1 just 1 it does not display all other zeros
Any idea why?
PHP automatically converts string into number if you are performing any numerical operation on it. But you can keep the order number in integer form and pad it with zeroes when necessary:
str_pad($lastcode['folio']+1, 15, "0", STR_PAD_LEFT);
echo $lastcode['folio'] should show you a result with the leading zeroes, but not $lastcode['folio']+1.
As soon as you do that +1, the result is no longer a string. The $lastcode['folio'] variable is converted to a number in order to do the arithmetic operation on it.
The leading zeroes are just formatting and don't need to be stored with the number. If you need an autoincrementing number, just use an autoincrement integer in MySQL, and format the number with leading zeroes when you print it out.
This will retrieve the number as a string to display it as text.
Later you can manipulate it on your coding environment.
SELECT CAST(document_number AS CHAR) FROM ...

Incorrect DECIMAL value when inserting MySQL 1

Name Type
price decimal(7,0)
INSERT INTO products (uid, item_code,item_name, brand_name,model_number,weight,dimension,description,category,quantity,price,imagename)
VALUES (1, '01','Lenovo','Lenovo laptop','qwqeiu145','50kg','5x9','Lenovo is the best','Computers & Accessories','2','$250.0000','lenovo');
THIS IS THE ERROR
1366 - Incorrect decimal value: '$250.0000' for column 'price' at row
1
You need to Change
$250.0000
To
250.0000
because it's decimal datatype. You can only insert numbers in it, while $ is a string
In decimal fields you can only insert numbers, you just need to remove the $.
Take care u declared the field with 0 decimals, any number after the dot will be ignored.

Missing leading Zero's when inserting Strings

I actually get very mad about PHP and SQLite3 and the way some of my strings behave there.
I try to save opening hours but in strings instead of numeric to prevent problem with leading zeros (and still have it now haha... -.-).
Hours and minutes have their own column but when I insert '0x' the zero is gone and whatever x is, is left in the database. :/
Im sure im just missing some little damn part somewhere...
I already checked the INSERT-statement but found nothing at all.
Example for an insert string:
INSERT INTO opening INSERT INTO opening (start_day, end_day, start_hour, start_minute, end_hour, end_minute) VALUES('Montag', 'Freitag', '00', '00', '01', '00')
But the output is:
11|Montag|Freitag|0|0|1|0
Part of the Code:
class Database_Opening_Hours extends SQLite3{
function __construct() {
if(!file_exists("../../data/opening_hours/opening_hours.sqlite")){
$this->open("../../data/opening_hours/opening_hours.sqlite");
$this->exec('CREATE TABLE opening (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, start_day STRING, end_day STRING, start_hour STRING, start_minute STRING, end_hour STRING, end_minute STRING)');
}
else{
$this->open("../../data/opening_hours/opening_hours.sqlite");
}
}
}
$db = new Database_Opening_Hours();
$insert = "INSERT INTO opening (start_day, end_day, start_hour, start_minute, end_hour, end_minute) VALUES('".htmlspecialchars($_GET["start_day"])."','".htmlspecialchars($_GET["end_day"])."','".$start_hour."','".$start_minute."','".$end_hour."','".$end_minute."')";
if($db->exec($insert)){
$db->close();
unset($db);
echo "Insert erfolgreich";
}else{
$db->close();
unset($db);
echo "Nicht wirklich...";
}
Fairly sure that the type of your columns is set to an integer (or any other number type) instead of TEXT.
Make sure to double check the column data type and actually dump the table for us to check if it's really set to TEXT.
This is caused by SQLite using dynamic typing. From the FAQ:
This is a feature, not a bug. SQLite uses dynamic typing. It does not enforce data type constraints. Data of any type can (usually) be inserted into any column. You can put arbitrary length strings into integer columns, floating point numbers in boolean columns, or dates in character columns. The datatype you assign to a column in the CREATE TABLE command does not restrict what data can be put into that column. Every column is able to hold an arbitrary length string.
And from the linked page (emphasis mine):
In order to maximize compatibility between SQLite and other database engines, SQLite supports the concept of "type affinity" on columns. The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data. It is just that some columns, given the choice, will prefer to use one storage class over another. The preferred storage class for a column is called its "affinity".
So SQLite is dynamically casting your values to integer.
I would suggest combining start_hour and start_minute into start_time (the same for the end_ fields) and storing the value in the format 00:00.
SQLite will store this 'as-is' but is smart enough to recognise a time value and allow you to perform date/time operations:
select time(start_time, '+1 hour') from opening
I had this problem with C/C++ because I did not quote the strings:
insert into test values('aa', 'bb');
use varchar instead of string, I had the same problem then I used varchar(length) and it worked fine

Convert string into float PHP

I have a column in a MySQL table that is a varchar. However the data in the column is representative of a float value. I want to know if there is an easy way to convert the string into a float without loosing the data. The problem is that there are leading zeros's that are complicating things for me.
Sample data:
"100"
"002"
"075"
"0300"
"0135"
Need to convert to:
1.00
0.02
0.75
0.300
0.135
When I try to convert by multiplying by a decimal the leading zeros are stripped off and 002 become 2.0 instead of 0.02. Complicating things more some of the string values are 3 characters and some are 4 characters.
I am using PHP 5.4.
Thanks for any assistance.
select cast(insert(your_column, 2, 0, '.') as decimal(10, 3))
from your_table
Try this code, it simply do casting. hope it helps.
$Myfloat = (float) $String;

BIGINT in MySQL Changes a my number? Why does it do this?

I'll try and keep this simple. I'm using a BIGINT data type on a MySQL database table. I have a function generating a unique random number that may span anywhere from 12 digits to 13 digest long.
When I insert into the database with a digit that is 12 digits in length, it enters it just fine,
but when I use a value that is 13 digits or longer, it seems like it rounds up or something. here is the
php
$defaultText = 'some string'; //just some string
$web_id = 12;
$element_id = 23112182735363; //case 1 (doesn't work)
//$element_id = 2311218333205; //case 2, does work ()
mysql_query("INSERT INTO tableName (web_id, element_id, content)
VALUES ($web_id, $element_id, '".mysql_real_escape_string($defaultText)."')");
results:
in case one, it inserts a slightly different number, usually rounds up for some reason.
in case two, it inserts the number just fine! Maybe someone can help shed some light on this mystery! Thanks again!
the big int datatype:
bigint(200)
Numbers lose their precision from PHP_INT_MAX onwards. See also: http://www.php.net/manual/en/reserved.constants.php#constant.php-int-max
After that they are turned into floats which have limited precision and causes weird rounding issues. The only way to support BIGINT in PHP is by using strings.
I assumed you were talking about a 32-bit server.
But in my server, PHP seemed not lose the precision.
echo(PHP_INT_MAX . "<br/>");
$big_int = -6174803190685607000;
echo($big_int . '<br/>');
output
9223372036854775807<br/>-6174803190685607000<br/>
Sadly I still got the precision losing. I guessed it might because i used 'i' in prepare statement of mysqli, but I could not prove it.

Categories