Belicoff
Belicoff

Reputation: 69

JSON LOOP through Result PHP

I have a Json file 'extract.json', i get content for loop through result, but my code return only one row, My script ==>

$filename  = "extract.json";
$str = file_get_contents($filename);
if($str!=null) {
    $decoded=json_decode($str,true);
    $compteur = 0;
    $compteur =  count($decoded);
    if(is_array($decoded)) {
        foreach($decoded as $value) {
            $var1= $value["val1"];
            $var2 = $value["val2"];
            $var3 = $value["val3"];
            $var4 = $value["val4"];
            $var5 = $value["val5"];....

            $injection_final = "INSERT INTO `Table`(
                `var1`,
                `var2`,
                `var3`,
                `var4`,
                `var5`,)
                VALUES
                ($var1,$var2,$var3,$var4,$var5)";
            $inj = mysqli_query($con1,$injection_final);
        }
    }
}

MY JSON FILE ==>

[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}...]

Upvotes: 0

Views: 66

Answers (4)

Mohammad Mudassir
Mohammad Mudassir

Reputation: 151

Simple change in this line from $var1,$var2,$var3,$var4,$var5)"; to

('$var1', '$var2', '$var3', '$var4', '$var5')";

Upvotes: 1

M A SIDDIQUI
M A SIDDIQUI

Reputation: 2205

<?php

$json_data = '[{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"},{"val1":"1","val2":"2","val3":"0000-00-00","val4":"1","val5":"1939-05-08"}]';
//uncomment the below line to read from file
//$json_data = file_get_contents("extract.json");
$array_data = json_decode($json_data, true);

for ($i = 0; $i < count($array_data); $i++) {
    $table_column_names = "";
    $table_column_values = "";
    foreach ($array_data[$i] as $coulmn_name => $field_value) {
        $table_column_names  .= "`$coulmn_name`, ";
        $table_column_values  .= "'$field_value', ";
    }
    $table_column_names = rtrim($table_column_names, ", ");
    $table_column_values = rtrim($table_column_values, ", ");
    $sql_query = "INSERT INTO `Table` ($table_column_names) VALUES ($table_column_values)";
    //excute query here -- like if i just print the query
    echo $sql_query."\n";
}

Here is what you need .. it will execute all rows Output :

INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')
INSERT INTO `Table` (`val1`, `val2`, `val3`, `val4`, `val5`) VALUES ('1', '2', '0000-00-00', '1', '1939-05-08')

Upvotes: 1

Wolfack
Wolfack

Reputation: 2769

The format of your JSON array is not correct. Please refer to the following link for an example:

http://www.w3schools.com/js/js_json_arrays.asp

Upvotes: 0

Suchit kumar
Suchit kumar

Reputation: 11859

The problem is in your sql, so it should not insert even a single row. you are using dates but haven't put them in quotes.Try like below:

 $filename  = "extract.json";
    $str = file_get_contents($filename);
    if($str!=null){
        /////.....rest of your code

                 $var1= $value["val1"];
                $var2 = $value["val2"];
                $var3 = $value["val3"];
                $var4 = $value["val4"];
                $var5 = $value["val5"];

                $injection_final = "INSERT INTO Table(var1,var2,var3,var4,var5,)
                VALUES($var1,$var2,'$var3',$var4,'$var5')";
                echo $injection_final."<br>";//use this query to insert

        ......
    }

Upvotes: 1

Related Questions