将数组插入MySQL

时间:2013-03-17 15:29:01

标签: php mysql arrays json

我非常困惑并一直在寻找。但正如标题所暗示我试图进入阵列。 我的问题是如何将此数组导入数据库?截至目前使用当前脚本,它只导入第一条记录而不是其他记录。在这里,我能够在同一个数组中导入其他值,这是一个JSON调用,并且已经被解码。

foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {
                $vehicle_dmg_id         = $vehicle_name['id'];
                $vehicle_dmg_name       = $vehicle_name['name'];
                $vehicle_dmg_value      = $vehicle_name['value'];
                $vehicle_dmg_faction_nc = $vehicle_name['faction']['nc'];
                $vehicle_dmg_faction_tr = $vehicle_name['faction']['tr'];
                $vehicle_dmg_faction_vs = $vehicle_name['faction']['vs'];
            }
     }
}

$add_dmg_veh = "INSERT INTO damage_given(character_number, vehicle_id,
vehicle_name, total_value, vehicle_faction_nc, vehicle_faction_tr,
vehicle_faction_vs) VALUES ('$character_id[$key]', '$vehicle_dmg_id', 
'$vehicle_dmg_name','$vehicle_dmg_value', '$vehicle_dmg_faction_nc', 
'$vehicle_dmg_faction_tr','$vehicle_dmg_faction_vs')";

6 个答案:

答案 0 :(得分:1)

虽然不建议将数组存储在数据库中,但您可以serialize()将数组存储在数据库中。基本上,PHP会将数组转换为特制的字符串,以后可以解释。

Serialize将其存储在数据库中,unserialize将其从数据库中取出时使用它

注意:我说不建议使用序列化,因为您的数据库不在First Normal Form中,特别是因为您在数据库中的特定条目中存储非原子值。对于这种情况,我建议创建一个单独的表,它可以单独存储这些值,并将两个表与外键链接在一起。

答案 1 :(得分:0)

你应该关注PDO_MySQL,你的插入字符串在循环之外,应该在它内部执行。

答案 2 :(得分:0)

你必须遍历数组并插入数组的每个字段。

foreach($array as $value) {
    // execute your insert statement here with $value
}

答案 3 :(得分:0)

首先,你不能像在做那样在MySQL中插入数组..就像迭代一样..

foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {
                $vehicle_dmg_id         = $vehicle_name['id'];
                $vehicle_dmg_name       = $vehicle_name['name'];
                $vehicle_dmg_value      = $vehicle_name['value'];
                $vehicle_dmg_faction_nc = $vehicle_name['faction']['nc'];
                $vehicle_dmg_faction_tr = $vehicle_name['faction']['tr'];
                $vehicle_dmg_faction_vs = $vehicle_name['faction']['vs'];


                // if you wants to use insert query then do here.
                $add_dmg_veh = "INSERT INTO damage_given(character_number, vehicle_id, 
                vehicle_name, total_value, vehicle_faction_nc, vehicle_faction_tr, 
                vehicle_faction_vs) VALUES ('$character_id[$key]', '$vehicle_dmg_id',
                '$vehicle_dmg_name', '$vehicle_dmg_value', '$vehicle_dmg_faction_nc',
                '$vehicle_dmg_faction_tr', '$vehicle_dmg_faction_vs')";

            }
    }
}

答案 4 :(得分:0)

尝试在数组中构建插入数据,然后将结果内嵌到单个查询中:

<?php
foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {

                $sql[] = "
                    (
                    ".$vehicle_name['id'].",
                    ".$vehicle_name['name'].",
                    ".$vehicle_name['value'].",
                    ".$vehicle_name['faction']['nc'].",
                    ".$vehicle_name['faction']['tr'].",
                    ".$vehicle_name['faction']['vs']."
                    )";
            }
    }
}

$query = "
INSERT INTO damage_given
(
character_number,
vehicle_id, 
vehicle_name, 
total_value, 
vehicle_faction_nc, 
vehicle_faction_tr, 
vehicle_faction_vs
) 
VALUES
".implode(",",$sql)."
";
?>

答案 5 :(得分:0)

这是我解决问题的方法!

$ stmt = $ dbh-&gt; prepare(             “INSERT INTO kills_vehicle(character_number,veh_id,veh_name,veh_total,veh_faction_nc,veh_faction_tr,veh_faction_vs)             VALUES(:char_id,:id,:vehname,:total_value,:faction_nc,:faction_tr,:faction_vs)“);

    foreach ($output as $key => $value) {
        if (isset($output[$key]["stats"]["play_time"]["vehicle"])) {
            $character_id[$key]    = $output[$key]["id"];
            $score_hit_count[$key] = $output[$key]["stats"]["kills"]["vehicle"];
            foreach ($score_hit_count[$key] as $row) {

                $stmt->bindValue(':char_id', $character_id[$key]);
                $stmt->bindValue(':id', $row[id]);
                $stmt->bindValue(':vehname', $row[name]);
                $stmt->bindValue(':total_value', $row[value]);
                $stmt->bindValue(':faction_nc', $row[faction][nc]);
                $stmt->bindValue(':faction_tr', $row[faction][tr]);
                $stmt->bindValue(':faction_vs', $row[faction][vs]);
                $stmt->execute();
            }
        }
    }