PHP / MySQL - 将JSON数据插入Bluehost服务器上的DB,空DB条目

时间:2015-11-01 19:13:37

标签: php mysql json database bluehost

这是一个一次性脚本,用于将JSON数据插入到Bluehost上的MySQL数据库中。我在循环内外使用了各种echo语句,以确保正确解析JSON信息并且循环按预期工作。 Bluehost帮助文件告诉我使用SQL语句而不是SQLi或DBO。

<?php
    $con = mysql_connect ("localhost:port", "username", "password");
    if (!$con) {
        die('Could not reach database: Error Code ' . mysql_error() . "<br>");
    } else {
        echo 'Connected to database. ' . "<br>";
    }
    mysql_select_db ("db_name", $con);

    $jsondata = file_get_contents('BFZ.json');
    $data = json_decode($jsondata, true);

    $cards = $data['cards'];

    // loop through the cards array and load each set of variables into the DB
    for($i = 0; $i <= count($cards); $i++) {
        $card_Name = $cards[$i]['name'];
        $card_ManaCost = $cards[$i]['manaCost'];
        $card_CMC = $cards[$i]['cmc'];

        // Clear the variable from its last use
        $card_Colors = "";

        // If the card has no color info, assign the text "Colorless"
        if ($cards[$i]['colors'] == "") {
            $card_Colors = "Colorless";
        // Else if the card has color info, convert the colors array into one long text variable
        } else {
            for($colorIndex = 0; $colorIndex < count($cards[$i]['colors']); $colorIndex++) {
                $card_Colors = $card_Colors . $cards[$i]['colors'][$colorIndex] . " ";
            }
        }

        // various bits to load into the DB
        $card_Type = $cards[$i]['type'];
        $card_Rarity = $cards[$i]['rarity'];
        $card_Text = $cards[$i]['text'];
        $card_Number = $cards[$i]['number'];
        $card_Power = $cards[$i]['power'];
        $card_Toughness = $cards[$i]['toughness'];
        $card_MultID = $cards[$i]['multiverseid'];
        $card_ID = $cards[$i]['id'];

        // insert the data into the cards table
        $sql = "INSERT INTO cards (card_ID, card_name, manaCost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)
        VALUES ('$card_ID', '$card_Name', '$card_ManaCost', '$card_CMC', '$card_Colors', '$card_Type', '$card_Rarity', '$card_Text', '$card_Number', '$card_Power', '$card_Toughness', '$card_MultID')";
    }
    if (!mysql_query($sql, $con)) {
        die('Error: ' . mysql_error());
    } else {
        echo "Data inserted correctly. <br>";
    }
    $con->close();
?>

最后一次“正确插入的数据”总是触发,但我的数据库只有一个条目,其中0表示存储在cmc,power,韧性和multiverseid以及颜色条目中的Colorless。 card_ID是我的主键,它是空白的。

数据库未正确设置或我在代码中遗漏了某些内容。我将包含我的数据库结构的屏幕截图,但我不确定这是否允许。排序规则在文本条目上设置为utf8_general_ci和Varchar,对数字设置为int或smallint。 JSON结构在这里:http://mtgjson.com/

直到两天前我才知道PHP和MySQL所以请原谅我,如果我错过了一些简单的东西。我读过的所有其他问题似乎都没有解决这个问题。

1 个答案:

答案 0 :(得分:1)

你的mysql_query在for循环之外,这就是为什么它只执行一次。它应该是

for($i = 0; $i <= count($cards); $i++) {
    //
    //...
    // insert the data into the cards table
    $sql = "INSERT INTO cards (card_ID, card_name, manaCost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)        VALUES ('$card_ID', '$card_Name', '$card_ManaCost', '$card_CMC', '$card_Colors', '$card_Type', '$card_Rarity', '$card_Text', '$card_Number', '$card_Power', '$card_Toughness', '$card_MultID')";

    //----> HERE instead
    if (!mysql_query($sql, $con)) {
       die('Error: ' . mysql_error());
    } else {
       echo "Data inserted correctly. <br>";
    }
}
$card_Type = mysql_real_escape_string($cards[$i]['type']);
$card_Rarity = mysql_real_escape_string($cards[$i]['rarity']);