简体   繁体   English

PDO从API请求中插入相同的字段数据

[英]PDO inserting identical field data from API request

I'm integrating my site with another website using their API. 我正在使用他们的API将我的网站与其他网站集成。 From one of the API calls, I want to insert the response into a table. 从其中一个API调用中,我想将响应插入表中。 I'm using cURL to query their API, and PDO to insert it into my MySQL database. 我正在使用cURL查询他们的API,并使用PDO将其插入我的MySQL数据库。

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. 然后,我有一个名为$uploadDetails的变量,它是API响应的数组,它包含文件的文件ID,文件名,文件大小和md5值。 I'm inserting the file ID into my database with this query: 我正在使用此查询将文件ID插入到我的数据库中:

// 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. 每个API响应都有一个唯一的文件ID,我肯定知道。 And I know I'm getting the correct and unique file ID back from the API call. 而且我知道我从API调用中获取了正确且唯一的文件ID。 I've verified that by echo'ing the response. 我通过回应响应验证了这一点。 However, for some reason, PDO keeps inserting this value under the rs_fileid column: 2147483647 . 但是,由于某种原因,PDO会继续在rs_fileid列下插入此值: 2147483647

For clarification, here's a sample response from the API call: 为了澄清,这是API调用的示例响应:

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. 如您所见, rs_fileid列的所有字段值都是相同的,它们应该是唯一的,并且API使用唯一值进行响应。

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. 注意, 2147483647是32位有符号整数的最大值 - 并且样本中的文件ID明显高于该值。 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 . 不确定PHP将如何处理它,因为这取决于你的PHP是32位还是64位(以及它是否难以尝试和启动数据),但MySQL会将数字限制在它所使用的字段范围内进入

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. 检查以确保您的数据库列是BIGINT或至少是UNSIGNED INT - 或者,除非您确实需要该列为数字,否则适当长度的VARCHAR字段也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM