Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MYSQL problem with BIGINT

Tags:

sql

php

mysql

I have a mysql database and a table to store stuff from twitter API. When i parse data with PHP from twitter i use mysql_query to insert data into the table.

I have a weird problem with the ID of the tweets:

For example the status update with ID 15861323074113537 (a tweet from google) is stored in the database as: 15861323074114000 (the last 4 digits are altered).

The php query is:

$sql = mysql_query("INSERT INTO $table (id,tw_text) VALUES ($id,'$tw_text')");

If i edit the record via phpmyadmin the correct value is stored (15861323074113537). The column is BIGINT.

So i guess something weird is going on with the php function mysql_query and the INSERT command.

Any ideas/solutions?

Thanks in advance

like image 262
Alex Avatar asked Oct 13 '22 19:10

Alex


2 Answers

ok found the problem, it wasn't code related. Twitter returns 2 IDs and one of those has 000 in the end. [id_str] => 15861323074113537 and [id] => 15861323074114000

Damn twitter API!

Sorry for the trouble :/

like image 62
Alex Avatar answered Nov 03 '22 04:11

Alex


PHP integers cannot be that large. When a number that doesn't fit into an integer is used, it is converted to a float. What you are probably seeing is that even the float is not precise enough to contain that many digits. Follow webbiedave's advice and keep it as a string to avoid rounding.

like image 26
ughoavgfhw Avatar answered Nov 03 '22 03:11

ughoavgfhw