PHP/MYSQL problem with BIGINT - php

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

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 :/

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.

Related

How do I PHP echo exactly what is stored in a MySQL field?

I feel like there is probably a very simple answer for this, but I've spent about 20 minutes searching and can't find anything.
Basically, I am using PHP to query a table and output the results as a list, using the primary key column (COL_1) of the table to create a link for each record that will bring the user to a detail page for that record. It works fine when the data in COL_1 is a straight-forward string such as "TEST". The edit link will then be detail.php?COL_1=TEST The detail page works by querying the database using the data passed by the link. So in this case it would do a select on the table where COL_1 = 'TEST' and return the correct record.
However, when new line characters are stored in COL_1 things get a bit complicated. For instance, if 'TEST\r\nTEST' is stored in COL_1, when the original query of the entire table is done, $row['COL_1'] for that line will give me 'TESTTEST', which gets passed to the detail page as detail.php?COL_1=TESTTEST, the detail page does a select on the table where COL_1 = 'TESTTEST', and it returns nothing.
However, if I manually link to detail.php?COL_1=TEST\r\nTEST the detail page will query on 'TEST\r\nTEST' and return the correct record.
So basically what I need is a way to do a query and have $row['COL_1'] return 'TEST\r\nTEST' instead of 'TESTTEST'. Does this make sense? How would I go about doing this?
As for why the table is set up like this, don't ask me. I didn't design it. I'd never design keys that can include line breaks like this. But I do have to interact with this table. Bah.
You should encode values that are passed in the URL:
echo urlencode("TEST\r\nTEST");
However, why would TEST\r\nTEST be a primary key? That's crazy. Maybe you need to rethink how you are doing things. Primary keys as integers work nicely.

HBase - How to query based on key-timestamp-id?

All-
New to HBase and I've finally been able to actually take data I was once storing in MySQL (about 50 million rows) and insert it into my HBase table.
I'm now trying to query this data based on the keys and am running into some problems.
Basically I have a key that is constructed like:
objectname-createdtime-customerid
Now I need to query based on the objectname and a range for the createdtime, does anyone know how I can do this? (I'm using PHP/Thrift, but I don't need it to be a specific answer to this)
I can query if I know the exact row/key, I just need to know how to specify a range now for the middle property.
Thanks in advance!
Use a scan where the start row is the one with key objectname-<min created time>-customerid and the stop row has key objectname-<max created time>-customerid.
http://wiki.apache.org/hadoop/Hbase/ThriftApi#Scanner_methods

Codeigniter db class truncating text on insert

I have a codeigniter controller that receives json content from an API and inserts it to a mysql longtext field unchanged.
Everything's been running smoothly for a while, but recently i've been noticing some of the content is being truncated. Here are two examples.
The table being inserted has three fields
id int
data longtext
date_added datetime
I am receiving the data and directly adding it to the database using the active record insert function. It looks like that
$this->db->insert('received', array('data' => $data, 'date_added' => date('Y-m-d H:i:s')));
Using the profiler, i monitored the queries and found two faulty ones:
INSERT INTO `received` (`data`, `date_added`) VALUES ('{\"status\":{\"lastMaintenanceAt\":0000,\"code\":304,\"period\":900},\"items\":[{\"permalinkUrl\":\"http://example.com\",\"updated\":0000,\"postedTime\":0000,\"summary\":\"Man\'s guarantees are like his words.\",\"title\":\"By: John\",\"content\":\"<p>Man’s guarantees are like his words.</p>\",\"id\":\"http://example.com\",\"actor\":{\"permalinkUrl\":\"\",\"displayName\":\"John\"}}],\"title\":\"Comments on: Banker refuses to give ‘Written Guarantee’\"}', '2012-04-08 00:28:29')
and
INSERT INTO `received` (`data`, `date_added`) VALUES ('{\"status\":{\"code\":304,\"period\":900,\"feed\":\"http://example.com\"},\"items\":[],\"title\":\"Comments on: Making her cry…\"}', '2012-04-08 00:49:35')
The queries seems alright. But only part of the JSON is making it to the table in the first case, it is truncated after "[...] refuses to give" and in the second after making her cry.
The queries are not returning an error and the date is being inserted properly. Moreover, if i copy the queries and execute them in a mysql command prompt, they will insert the full text.
These queries a one a few of hundreds of other successful ones.
Any idea what might be the problem?
thank you
All of the faulty queries share one thing in common: they've got "special characters" which MS Word inserts, e.g. ‘, ’ and …. If you can convert / remove these, then your problems should go away.
Update
For a CodeIgniter solution to this, you could load the Text Helper library and then use the ascii_to_entities function on any strings, e.g.
$this->load->helper('text');
$desc = ascii_to_entities($desc);

php manual increment membership no

i've got hundreds of people joining my website and create an membership id for them. i just created a new column in the database called user_no.
whats the mysql query for incrementing the membership no.
is it possible to start with AE then numbers ie: AE0001, AE0002, .... and it starts with 4 number not AE1, AE2..
mysql query:
UPDATE user SET user_no=..??
and on PHP side, how do i increment it? if there is a new member join in.
$db->query("INSERT INTO user (user_no) VALUES(AE'$user_no')");
Why not use an autoincrement field and append AE to it? Autoincrement will be carried out by MySQL so you don't have to worry about it in PHP : http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
And to display your user key in the format of AE0001 you can do
$id = 'AE' . str_pad($autoincrementid, 5, "0", STR_PAD_LEFT);
http://php.net/manual/en/function.str-pad.php
I agree with everyone else who is saying that you should simply use the MySQL Auto-increment feature. That's what it's there for.
It is possible to write your own, possibly using MySQL's MAX() function to find the highest value of a field currently in the table. However unless you're using some very robust transactional code, there is always the danger that this method will result in duplicate records being created when two users create accounts at exactly the same time.
The amount of code required to avoid this is not small, and if you're inexperienced enough not to see the benefits of using Auto-increment then you're unlikely to get it right.
The whole point of Auto-increment is to save you from having to implement all that code every time.
In addition, it is highly recommended for performance reasons to use an integer value for your primary key. Sure, you can display it as "AE" . $id, but you should store it as an integer on the database.

AES, MySql and Headaches

I am working on a checkout page for an ecommerce site. In said site, people input their credit card information and it is saved to the order in an AES encrypted format. This worked fine when all they supported was VISA. Now that they support MasterCard and Discover (16 digit numbers), it no longer works. The insert query looks like this:
"insert into order_master set ccnum = AES_ENCRYPT(:ccnum, 'password') ...";
i have also tried the alternative format:
"insert into order_master (ccnum, ...) values (AES_ENCRYPT(:ccnum, 'password')..)";
Both to no avail. Could someone please try to point me into the right direction as to what I am doing wrong? I have gone so far as to spit out the entries query to make sure the values are inserted correctly, etc and nothing. it's quite frustrating and i am hoping someone out there can help me. thanks!
As it turned out, the client had the data type of the column set to varchar (20) - which failed to capture all of the binary data (of only card numbers with 16 characters before encryption). I had him change the length to 255 and it works great now. Thanks for all of your help/suggestions!
I don't see anyting wrong with your sql but from the mysql documentation ...
If AES_DECRYPT() detects invalid data
or incorrect padding, it returns NULL.
However, it is possible for
AES_DECRYPT() to return a non-NULL
value (possibly garbage) if the input
data or the key is invalid.
This would explain by you can decrypt you get a value .. but if you encrypt and you get a null value returned, one of the two input values is probably null.

Categories