PHP PDO绑定问题

时间:2013-06-26 12:55:31

标签: php mysql pdo

好吧,我认为我已经尝试了所有内容并阅读了分配,但我似乎无法找到任何解决方案。我正在尝试创建一个打开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
;";

然后下面的代码应该加载到db:

$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保持不变。我已经通过删除'on duplicate'语句对此进行了测试,并且每次执行查询时都会显示此错误:

  

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

我已经反复检查了参数,并且在代码中看到它实际上回显了订单ID,所以我绝对确定订单ID正在改变。其他参数似乎改变得很好,结果行包含除订单ID之外的最后一个XML'row'所做的所有其他数据。

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

解决方案

  

@deceze 2147483647巧合地是32位整数的最高可能数。我猜你试图存储一个大于那个的数字,并且最大限度地存在。

1 个答案:

答案 0 :(得分:-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 />");
    }
}

这样的事情可能会绑定循环外的参数,然后只需在循环中分配变量。