1

好的,我想我已经尝试了一切并阅读了分配,但我似乎找不到任何解决方案。我正在尝试创建一个脚本来打开一个 XML 文件并将其读取到我的数据库中。我发现了 PHP PDO,所以我决定尝试使用它,但这让我很生气。

我正在创建以下数据库表:

$sql = "
DROP TABLE IF EXISTS eve_market_data;
CREATE TABLE eve_market_data
(
    `order` INT NOT NULL,
    `region` INT NOT NULL,
    `station` INT NOT NULL,
    `range` INT,
    `price` FLOAT(12,2) NOT NULL,
    `vol_remain` INT,
    `min_volume` INT,
    `expires_date` DATE,
    `reported_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `sell` INT NOT NULL,
    PRIMARY KEY(`order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

以下查询应该足以将数据加载到数据库中(据我所知):

$sql = "
INSERT INTO `eve_market_data` 
(`order`, `region`, `station`, `range`, `price`, `vol_remain`, `min_volume`, `expires_date`, `reported_time`,`sell`)
VALUES
(
    :orderid,
    :regionid,
    :stationid,
    :range,
    :price,
    :volremain,
    :minvolume,
    :expires,
    :reportedtime,
    :sell
)
ON DUPLICATE KEY UPDATE 
    `range` = :range,
    `price` = :price,
    `vol_remain` = :volremain,
    `min_volume` = :minvolume,
    `expires_date` = :expires,
    `reported_time` = :reportedtime
;";

然后以下代码应将其加载到数据库:

$sth = $db->prepare($sql);

for( $i = 0; $i < count( $so->order ); $i++ )
{
$order = $so->order[$i];

echo "[{$i}] Inserting... " . $order['id'];

$sth->bindParam(':orderid', $orderid = (int) $order['id']);
$sth->bindParam(':regionid', $regionid = (int) $order->region);
$sth->bindParam(':stationid', $stationid = (int) $order->station);
$sth->bindParam(':range', $range = (int) $order->range);
$sth->bindParam(':price', $price = (float) $order->price);
$sth->bindParam(':volremain', $volremain = (int) $order->vol_remain);
$sth->bindParam(':minvolume', $minvolume = (int) $order->min_volume);
$sth->bindParam(':expires', $expires = (string) $order->expires);
$sth->bindParam(':reportedtime', $reporttime = date('Y') . "-" . (string) $order->reported_time);
$sth->bindParam(':sell', $sell = 1);

if( $sth->execute() ) echo '<br/>Executed';

echo "<br/><br/>";

if( $i === 7 ) break;
}

但由于某种原因,每个查询的订单 ID 保持不变。我已经通过删除“重复”语句对此进行了测试,每次执行查询时都会显示此错误:

警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[23000]: 完整性约束违规:1062 第 65 行* /ECProcessor.php中键 'PRIMARY' 的重复条目 '2147483647'

我一遍又一遍地检查了参数,从代码中可以看出,它实际上回显了订单 ID,所以我绝对确定订单 ID 正在改变。其他参数似乎变化得很好,结果行包含最后一个 XML“行”所做的所有其他数据,但订单 ID 除外。

有谁知道问题是/可能是什么?提前致谢。

解决方案

@deceze 2147483647 巧合的是 32 位整数的最大可能数。我猜你正在尝试存储一个比这更大的数字并且它在那里最大化。

4

1 回答 1

-1
$sth = $db->prepare($sql);

$sth->bindParam(':orderid', $orderid);
$sth->bindParam(':regionid', $regionid);
$sth->bindParam(':stationid', $stationid);
$sth->bindParam(':range', $range);
$sth->bindParam(':price', $price);
$sth->bindParam(':volremain', $volremain);
$sth->bindParam(':minvolume', $minvolume);
$sth->bindParam(':expires', $expires);
$sth->bindParam(':reportedtime', $reporttime);
$sth->bindParam(':sell', $sell);

for( $i = 0; $i < count( $so->order ); $i++ ) {
    $order = $so->order[$i];
    echo "[{$i}] Inserting... " . $order['id'];

    $orderid = (int) $order['id'];
    $regionid = (int) $order->region;
    $stationid = (int) $order->station;
    $range = (int) $order->range;
    $price = (float) $order->price;
    $volremain = (int) $order->vol_remain;
    $minvolume = (int) $order->min_volume;
    $expires = (string) $order->expires;
    $reporttime = date('Y') . "-" . (string) $order->reported_time;
    $sell = 1;

    try {
        $sth->execute();
        echo "executed successfully <br />";
    catch Exception $e) {
        die("Oh noes! There's an error in the query!<br />");
    }
}

像这样的东西可能会在循环之外绑定参数,然后在循环内分配变量。

于 2013-06-26T13:01:26.310 回答