Value being inserted as 2147483647 into database - php

All phone numbers I am trying to enter into my database are being inserted as 2147483647.
The database field is an integer(11).
Before the phone number is inserted, it goes through the following code in order to remove all spaces, dashes, and brackets:
if (!empty($hphone)) $phone = $hphone;
else if (!empty($HomePhone)) $phone = $HomePhone;
else if (!empty($Phone1)) $phone = $Phone1;
$phone = preg_replace("/[^0-9]/", "", $phone);
Why is it inserting the phone number as 2147483647 every time, no matter what the phone number is?

If you can, convert the phone number to a VARCHAR field, don't store it as a signed (or unsigned) numeric value (int, bigint, double, etc...).
In this case, the limit for signed INT in MySQL of 2147483647 is what is causing your issue. Anything larger inserted into this field will be capped at that maximum value.
For example the phone number 555-555-5555 if bigger than that limit (5555555555 >2147483647), as such storing it would result in the max value 2147483647.
I also recommend not storing it as a BIG INT or any other numeric type. How will you handle extension or special encoded characters like:
+02 112020 10020
1-333-232-9393 x203
BTW: don't know if the first is real non-US number format, but you get the idea
Also, phone numbers with relevant leading 0's would be have some of it lost no mater how large the number:
021-392-9293
Would be the number 213929293

if you want to store it as a number use bigint because int has it's max value equal to 2147483647.
So whatever number you try to store that is higher than 2147483647 will be stored as 2147483647.
Here is some reference for mysql types:
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html

I am also facing this type of problem and I have solved that problem after
Updating datatype From 'INT' TO 'BIGINT'.
#php #mysql #database #sql

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 ...

MySQL Decimal 9.99999999

I am getting the currency from google finance using a script and then I am writing the data into a database.
However when I do, I get 9.99999999 instead of what I sent over. I have my InnoDB set to DECIMAL (9,8).
$conn->set_charset('utf8');
$add_currency_query = "INSERT INTO `currency_table`
(`currency_rate`, `time_fetched`, `service`)
VALUES ('$converted_amount', UTC_TIMESTAMP(), 'google-finance')";
And my PHP
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$converted_amount = preg_replace('/[^0-9\.]/', null, $get[0]);
Any tips on how to fix this/am I doing something wrong?
DECIMAL (9,8) means 9 digits in total and 8 of that after the decimal point.
So what is the max number of that decimal? it is 9.99999999. You get that number when the one you sent is bigger than it is possible to store. MySQL just stores as high as possible when the value overflows.
DECIMAL (9,8) means you have 9 symbols in total, 8 of those are after the dot. This means you can store only numbers up to 9.99999999 if you save anything above it it probably saves as 9.99999999
Suggestion: change your field to be something like DECIMAL (20,8)

Mysql Changing My Float Values

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!

Wrong number saved in database - Yii framework

This may sound a little stupid but I need some help.I should save some records in the database as follow by accessing the following link:
http://localhost/whatsapp/index.php?r=users/create&mobile=012345678900
This is what I have in the controller:
public function actionCreate()
{
$model=new Users;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
echo "hello"." hii";
if(!isset($_POST['number']))
{
echo "aho";
$model->number="012345678900";
//$model->number=$_POST['number'];
echo 'bye';
//$model->attributes=$_POST['Users'];
if($model->save())
echo "done";
return "Doneeee";
}
}
The data is saved correctly except the column number.Always a certain number is saved as follows:
2147483647
Any dea why this is happening?!
Sql engine generate this kind of number when data type is not bigint and length of col is less than the length of number you entered.
In your Users table change the type of number to varchar and set its length to 15 or 20 or whatever you want.
Try this query
ALTER TABLE `users` CHANGE `number` `number` VARCHAR( 15 )
Note:- You are getting this problem because you have set your number field as int type. and the biggest value int type can hold is what you are getting in the database. So changing it to the varchar type will solve the problem.
A number starting with 0 is regarding as the OCTAL representation of the number
http://en.wikipedia.org/wiki/Octal
What is happening here is that the octal number, when represented as a "normal" or "decimal" number, gets to large, and overflows in the space provided (kind of like the mileage counter in a motor vehicle). Therefore, when the number gets saved, it gets converted into a number, then overflows.
The correct treatment is to regard this as a string, as suggested by #Let me see

PHP: Converting big integer from string to int with overflow

PHP does not support unsigned ints. Is there a way to convert a string representation of an unsigned integer into a signed integer with overflow?
Example:
On a 32 bit system, PHP can store int values <= 2147483647. I want a way to convert the string "2147483648" to integer, causing it to overflow to -2147483648 instead of being reduced to 2147483647.
Why do I want to do this?
I store IPv4 addresses in a database as unsigned int (32 bits). I want to do binary operations on the addresses in PHP to check for subnets. This needs to be done on every request, so it needs to be quick. Therefore it seems better to store the IP address as an unsigned int rather than storing a string which will have to be converted back and forth.
here is the workaround.
<?php
$unsignedString = "3000000000";
echo "unsigned string: ".$unsignedString."<br />";
$signedInt = intval(doubleval($unsignedString));
echo "signed int: ".$signedInt."<br />";
?>
The fastest and easiest way to do this is to get your RDBMS to do it somehow.
You can find out what the size of an integer is in PHP by checking the value of the predefined constant PHP_INT_SIZE. This will be 4 if running on a 32-bit system, 8 if running on a 64-bit system.
I suggest that you populate a variable with for example
$smallIntegers = intval(PHP_INT_SIZE == 4);
and then in your query use something like this:
SELECT
...,
CASE
WHEN :smallIntegers: = 1 AND IPAddress > 2147483647 THEN IPAddress - 4294967296
ELSE IPAddress
END AS IPAddress,
...
...
Could you store the IP address in an integer array, one segment of the IP to each part of the array so the max value would end up as 255 stored in anyone one integer:
$ipAddress = array(127, 0, 0, 1);

Categories