简体   繁体   English

嵌套for循环以将JSON插入php的SQL表的同一行中

[英]nested for loops to insert JSON into same row in SQL table in php

I have a json file with 20 arrays(some with arrays inside arrays), I want to insert 3 different arrays into the same row on my table. 我有一个包含20个数组的json文件(有些在数组中包含数组),我想在表的同一行中插入3个不同的数组。 Since doing each query separate would insert each array into a different row I tried to use nested for loops. 由于单独执行每个查询会将每个数组插入不同的行,因此我尝试使用嵌套的for循环。 The results for my code below as is were 1) only the second for loop values are inserted and the first for loop values are NULL 2) if i added a query at the end of the first for loop I get two rows,the first row with the values from the first for loop inserted and the second row contains the values from my second loop inserted. 我下面的代码的结果是1)仅插入第二个for循环值,而第一个for循环值是NULL 2)如果我在第一个for循环的末尾添加了查询,则会得到两行,第一行插入了第一个for循环的值,第二行包含了我插入的第二个循环的值。 The values itself dont matter necessarily but my question is how can I insert different arrays of a JSON file into a single row on an sql table. 值本身并不一定重要,但是我的问题是如何将JSON文件的不同数组插入sql表的单行中。

<?php
error_reporting(E_ALL);
$dir = "--------------------";
$connect = mysqli_connect("-----", "----", "","------");
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        foreach(glob("*.json") as $filename) {
            $data = file_get_contents($filename);
            $testing = json_decode($data, true);
            foreach ($testing[0][0] as $Comments) {
                $sql = "INSERT INTO servers_updated (Model)  VALUES ('".$Comments["Model"]."')";

                foreach($testing[3][1] as $row2) {
                    $sql = "INSERT INTO servers_updated (TotalCores)
                              VALUES ('".$row2['/redfish/v1/Systems/1/Processors/1/']['TotalCores']."')";

                    if ($connect->query($sql) === TRUE) {
                        echo "Data entered";
                    } else {
                        echo "Error  entering data: " . $connect->error;
                    }
                }
            }
        }
        $connect->close();
    }
}
?>

You should have only one INSERT if you only want one row for both these pieces of data, and that can use the data from both the foreach loops like this. 如果您只希望对这两条数据都使用一行,则应该只有一个INSERT,并且这样可以使用两个foreach循环中的数据。

You should also be using Prepared and Parameterised statements, that will improve performance in this code as you only need to prepare the query once, therefore is will only be compiled once by the database. 您还应该使用Prepared和Parameterized语句,这将提高此代码的性能,因为您只需要准备一次查询,因此将仅由数据库编译一次。 You then amend the parameters each time round the loop before doing the insert. 然后,您可以在每次插入循环之前在循环中修改参数。

<?php
error_reporting(E_ALL);
$dir = "--------------------";
$connect = mysqli_connect("-----", "----", "","------");
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {

        // only need one query, 
        // and it can be prepared once if done here outside all the loops
        $sql = "INSERT INTO servers_updated (Model, TotalCores) VALUES (?,?)";
        $stmt = $connect->prepare($sql);

        foreach(glob("*.json") as $filename) {
            $data = file_get_contents($filename);
            $testing = json_decode($data, true);

            foreach ($testing[0][0] as $Comments) {

                foreach($testing[3][1] as $row2) {
                    $stmt->bind_param('ss', $Comments["Model"],
                                            $row2['/redfish/v1/Systems/1/Processors/1/']['TotalCores']);

                    if ( $stmt->execute() ){
                        echo "Data entered";
                    } else {
                        echo "Error  entering data: " . $connect->error;
                    }
                }
            }
        }
        $connect->close();
    }
}
?>

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

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