I'm integrating my site with another website using their API. From one of the API calls, I want to insert the response into a table. I'm using cURL to query their API, and PDO to insert it into my MySQL database.
Here's the relevant code:
// Upload the file to RapidShare
$uploadID = mt_rand(1000000000, 9999999999);
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?uploadid=' . $uploadID;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$postFields = array('sub' => 'upload',
'login' => 'mylogin',
'password' => 'mypassword',
'uploadid' => $uploadID,
'filename' => $filename,
'filecontent' => '@' . $targetDir . '/' . $id);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
if(!$resp = curl_exec($ch)) {
error('upload call failed');
}
Then, I have the variable called $uploadDetails
which is an array of the API response which holds the file ID, file name, file size and md5 value of the file. I'm inserting the file ID into my database with this query:
// Database shinanagans
try {
$dbh = new PDO('mysql:host=localhost;dbname=mytable', 'root', '');
$query = 'INSERT INTO `files` (f_id, s_id, u_id, name, size, uploaded, rs_fileid)
VALUES (?, (SELECT s_id FROM `servers` WHERE name = ? LIMIT 1), (SELECT u_id FROM `users` WHERE username = ?), ?, ?, ?, ?)';
$sth = $dbh->prepare($query);
$sth->execute(array($id, $server, 11, $uploadDetails[1], $uploadDetails[2], date('c'), $uploadDetails[0]));
$dbh = null;
} catch(PDOException $e) {
error($e->getMessage());
}
Each API response has a unique file ID, I know that for sure. And I know I'm getting the correct and unique file ID back from the API call. I've verified that by echo'ing the response. However, for some reason, PDO keeps inserting this value under the rs_fileid
column: 2147483647
.
For clarification, here's a sample response from the API call:
Array
(
[0] => 3294575677
[1] => Untitled_1.png
[2] => 478923
[3] => ae83e53e5bf26406715daed0d44adc45
)
And here's the relevant database row:
As you can see, all the field values for the rs_fileid
column are the same, when they should be unique, and the API is responding with a unique value.
Anyone know what could be going on? Thank you.
Note, 2147483647
is the maximum value for a 32-bit signed integer -- and the file ID in your sample is significantly above that. Not sure about how PHP will handle it, as that depends on whether your PHP is 32- or 64-bit (and whether it bothers to try and intify the data), but MySQL will clamp the number to the range of the field it goes into.
Check to make sure your database column is a BIGINT
or at least UNSIGNED INT
-- or, unless you really need that column to be numeric, a VARCHAR
field of appropriate length would work as well.