ホームページ  >  記事  >  バックエンド開発  >  phpがjsonデータをmysqlデータにインポートできない問題の解決策

phpがjsonデータをmysqlデータにインポートできない問題の解決策

WBOY
WBOYオリジナル
2016-06-13 12:07:241205ブラウズ

PHP が json データを mysql データにインポートできませんでした
まず、私が行ったことの原理について説明します。
1. まず、配列から必要なデータを抽出した後、
2. 、構成 私が SQL ステートメントの VALUES の一部である場合、抽出された部分を配列として保存します。
3. 上記で抽出された VALUES は配列であるため、for ループを使用して SQL ステートメントを実行します。しかし、i が定義されていないというメッセージが表示されます
見てみましょう 何が起こっているのか見てみましょう

<?php<br />require ('inc/config.php');<br />require (MYSQL);<br />$data ='[{"date":"2014-09-06","num":151,"area":18017.24},{"date":"2014-09-07","num":103,"area":11703.9},{"date":"2014-09-08","num":66,"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":43,"area":5056.19},{"date":"2014-09-15","num":121,"area":12867.43},{"date":"2014-09-16","num":93,"area":8755.81},{"date":"2014-09-17","num":80,"area":9035.69},{"date":"2014-09-18","num":158,"area":17613.12},{"date":"2014-09-19","num":112,"area":12180.37},{"date":"2014-09-20","num":74,"area":8614.02},{"date":"2014-09-21","num":95,"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":92,"area":9384.07},{"date":"2014-10-03","num":70,"area":7780.39},{"date":"2014-10-04","num":71,"area":7620.61},{"date":"2014-10-05","num":44,"area":4672.24}]';<br /><br />$array = json_decode($data, true);<br />//print_r ($array);<br />$values = array();  <br /><br />foreach ($array as $k => $v) {<br />	//echo "('" . $k . "', '" . $v . "')";<br />	//echo $v."<br>";<br />	//echo $v['date'].$v['num'].$v['area']."<br>";<br />	$values[] = "('" . $v['date'] . "', " . $v['num'] . ", " . $v['area'] . ")";<br />} <br /><br />for ($i=0; $i<=100; $i++) {<br />	$q = "INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, $values[i], NOW())";<br />}<br />$r = mysqli_query ($conn, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($conn));<br />if (mysqli_affected_rows($conn) == 1) {<br />	echo '这条数据已经添加成功';<br />} else {<br />	echo '程序发生错误,请重新添加'; 			<br />}<br /><br />?>

------解決策のアイデア------------ ----------
<br />$data ='[{"date":"2014-09-06","num":151,"area":18017.24},{"date":"2014-09-07","num":103,"area":11703.9},{"date":"2014-09-08","num":66,"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":43,"area":5056.19},{"date":"2014-09-15","num":121,"area":12867.43},{"date":"2014-09-16","num":93,"area":8755.81},{"date":"2014-09-17","num":80,"area":9035.69},{"date":"2014-09-18","num":158,"area":17613.12},{"date":"2014-09-19","num":112,"area":12180.37},{"date":"2014-09-20","num":74,"area":8614.02},{"date":"2014-09-21","num":95,"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":92,"area":9384.07},{"date":"2014-10-03","num":70,"area":7780.39},{"date":"2014-10-04","num":71,"area":7620.61},{"date":"2014-10-05","num":44,"area":4672.24}]';<br /> <br />$array = json_decode($data, true);<br />$values = array();  <br /> <br />foreach ($array as $k => $v) {<br />    $values[] = "'" . $v['date'] . "', " . $v['num'] . ", " . $v['area'] . "";<br />} <br /> <br />for ($i=0; $i<count($values); $i++) {<br />    $q = "INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, ".$values[$i].", NOW())";<br />    echo $q.'<br>';<br />}<br />

テストしましたが問題ありません。

<br>INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-06', 151, 18017.24, NOW())<br>INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-07', 103, 11703.9, NOW())<br>INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-08', 66, 7378.92, NOW())<br>INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-09', 192) 、22160.96、NOW())<br>INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-10', 183, 22059.72, NOW())<div class="clear"></div>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。