PHP to import JSON data to MySQL data failure solution

Source: Internet
Author: User
PHP failed to import JSON data into MySQL data
First of all, let me make the following principles:
1, first convert the JSON into an array;
2, in the array of I need to be extracted from the data, composition I if the SQL statement part of the values, I extracted this part of an array;
3, because the above extracted values is an array, I use a For loop to execute the SQL statement, but the hint I did not define
Let's see what's going on.
 Require (' inc/config.php ');
Require (MYSQL);
$data = ' [{' Date ': ' 2014-09-06 ', ' num ': 151, ' area ': 18017.24},{' date ': ' 2014-09-07 ', ' num ': 103, ' area ': 11703.9},{' Date ' : "2014-09-08", "num": $, "area": 7378.92},{"date": "2014-09-09", "num": 192, "area": 22160.96},{"date": "2014-09-10", " Num ': 183, ' area ': 22059.72},{' date ': ' 2014-09-11 ', ' num ': 184, ' area ': 21212.68},{' date ': ' 2014-09-12 ', ' num ': 119, ' area ": 13960.93},{" date ":" 2014-09-13 "," num ": 0," area ": 0},{" date ":" 2014-09-14 "," num ": +," area ": 5056.19},{" date ":" 2014-09-15 "," num ": 121," area ": 12867.43},{" date ":" 2014-09-16 "," num ":" The "," area ": 8755.81},{" date ":" 2014-09-17 "," num ":", "area": 9035.69},{"date": "2014-09-18", "num": 158, "area": 17613.12},{"date": "2014-09-19", "num":, "area" : 12180.37},{"date": "2014-09-20", "num":, "area": 8614.02},{"date": "2014-09-21", "num": Up, "area": 10305.27},{"date ":" 2014-09-22 "," num ": 258," area ": 30695.9},{" date ":" 2014-09-23 "," num ": 155," area ": 17457.88},{" date ":" 2014-09-24 ", "num": 148, "area": 16792.02},{"date": "2014-09-25", "num": 167, "area": 20203.15},{"date": "2014-09-26", "num": 174, " Area ": 20055.03},{" datE ":" 2014-09-27 "," num ": 138," area ": 15927.18},{" date ":" 2014-09-28 "," num ": 175," area ": 19372.63},{" date ":" 2014-09-29 "," num ": 277," area ": 30226.38},{" date ":" 2014-09-30 "," num ": 311," area ": 33082.01},{" date ":" 2014-10-01 "," num ": 88," Area: 9456.33},{"date": "2014-10-02", "num": 9384.07},{"date": "2014-10-03", "num": +, "area": 7780.39},{" Date ":" 2014-10-04 "," num ":," area ": 7620.61},{" date ":" 2014-10-05 "," num ": $," area ": 4672.24}] ';

$array = Json_decode ($data, true);
Print_r ($array);
$values = Array ();

foreach ($array as $k = = $v) {
echo "('". $k. "', '" . $v. "')";
echo $v. "
";
echo $v [' Date ']. $v [' num ']. $v [' area ']. "
";
$values [] = "('". $v [' Date ']. "', " . $v [' Num ']. ", " . $v [' area ']. ")";
}

for ($i =0; $i <=100; $i + +) {
$q = "INSERT into F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, $values [i], now ())";
}
$r = Mysqli_query ($conn, $q) or Trigger_error ("query: $q \ n
MySQL Error: ". Mysqli_error ($conn));
if (mysqli_affected_rows ($conn) = = 1) {
Echo ' This piece of data has been added successfully ';
} else {
Echo ' program error, please re-add ';
}

?>

------to solve the idea----------------------

$data = ' [{' Date ': ' 2014-09-06 ', ' num ': 151, ' area ': 18017.24},{' date ': ' 2014-09-07 ', ' num ': 103, ' area ': 11703.9},{' Date ' : "2014-09-08", "num": $, "area": 7378.92},{"date": "2014-09-09", "num": 192, "area": 22160.96},{"date": "2014-09-10", " Num ': 183, ' area ': 22059.72},{' date ': ' 2014-09-11 ', ' num ': 184, ' area ': 21212.68},{' date ': ' 2014-09-12 ', ' num ': 119, ' area ": 13960.93},{" date ":" 2014-09-13 "," num ": 0," area ": 0},{" date ":" 2014-09-14 "," num ": +," area ": 5056.19},{" date ":" 2014-09-15 "," num ": 121," area ": 12867.43},{" date ":" 2014-09-16 "," num ":" The "," area ": 8755.81},{" date ":" 2014-09-17 "," num ":", "area": 9035.69},{"date": "2014-09-18", "num": 158, "area": 17613.12},{"date": "2014-09-19", "num":, "area" : 12180.37},{"date": "2014-09-20", "num":, "area": 8614.02},{"date": "2014-09-21", "num": Up, "area": 10305.27},{"date ":" 2014-09-22 "," num ": 258," area ": 30695.9},{" date ":" 2014-09-23 "," num ": 155," area ": 17457.88},{" date ":" 2014-09-24 ", "num": 148, "area": 16792.02},{"date": "2014-09-25", "num": 167, "area": 20203.15},{"date": "2014-09-26", "num": 174, " Area ": 20055.03},{" datE ":" 2014-09-27 "," num ": 138," area ": 15927.18},{" date ":" 2014-09-28 "," num ": 175," area ": 19372.63},{" date ":" 2014-09-29 "," num ": 277," area ": 30226.38},{" date ":" 2014-09-30 "," num ": 311," area ": 33082.01},{" date ":" 2014-10-01 "," num ": 88," Area: 9456.33},{"date": "2014-10-02", "num": 9384.07},{"date": "2014-10-03", "num": +, "area": 7780.39},{" Date ":" 2014-10-04 "," num ":," area ": 7620.61},{" date ":" 2014-10-05 "," num ": $," area ": 4672.24}] ';

$array = Json_decode ($data, true);
$values = Array ();

foreach ($array as $k = = $v) {
$values [] = "'". $v [' Date ']. "', " . $v [' Num ']. ", " . $v [' area ']. "";
}

for ($i =0; $i
$q = "INSERT into F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1,". $values [$i]. ", Now ())";
echo $q. '
';
}

Tested, no problem.

 
INSERT into F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, ' 2014-09-06 ') , 151, 18017.24, now ())
INSERT in F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, ' 2014-09-07 ', 10 3, 11703.9, Now ())
INSERT in F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, ' 2014-09-08 ', 66, 737 8.92, now ())
INSERT to F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, ' 2014-09-09 ', 192, 22160.96 , now ())
INSERT to F_chengjiao (city_id, Fang_time, Taoshu, area, Os_time) VALUES (1, ' 2014-09-10 ', 183, 22059.72, NO W ())
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.