ホームページ >バックエンド開発 >PHPチュートリアル >PHP が json データを mysql データにインポートできない

PHP が json データを mysql データにインポートできない

WBOY
WBOYオリジナル
2016-06-23 13:48:07951ブラウズ

まず、私が行うことの原理について説明します:
1. まず json を配列に変換します。
2. 配列内のデータを抽出した後、抽出された部分を SQL ステートメントの VALUES の一部として形成します。配列として;
3. 上記で抽出した VALUES は配列であるため、for ループを使用して SQL ステートメントを実行しましたが、i が定義されていないというメッセージが表示されました
何が起こっているのか見てみましょう


返信ディスカッション (解決策)

ここで、グループのヘルプを入力していくつかの問題を見つけましたが、正常に実行された後、追加されたデータは、このデータにまだ問題があることがわかりました。は四捨五入されており、小数点はありません
コードを貼り付けます

<?phprequire ('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":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}]';$array = json_decode($data, true);//print_r ($array);$values = array();  foreach ($array as $k => $v) {	//echo "('" . $k . "', '" . $v . "')";	//echo $v."<br>";	//echo $v['date'].$v['num'].$v['area']."<br>";	$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<br />MySQL Error: " . mysqli_error($conn));if (mysqli_affected_rows($conn) == 1) {	echo '这条数据已经添加成功';} else {	echo '程序发生错误,请重新添加'; 			}?>

小数点 データベースは int (10) で設定されています。今は 10 進数 (10,2) に設定されています。 for ($i=0; $i $q = "INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, $values [$i ], NOW())";

}

for ($i=0; $ ib7e1832d2775772f12dfe8aaab5d0837MySQL Error: " . mysqli_error($conn));
    if (mysqli_affected_rows($conn) == 1) {
        echo '这条数据已经添加成功';
    } else {
        echo '程序发生错误,请重新添加';            
    }
}



修正后发现数据不能全部添加,数据一共有30条,但是添加到数据的时候,数据就到了9月16号那天就结束了(数据库本身有一点数据),自增的id也正好是30,这是什么情况???  同时我想问下,还有什么方法可以降json数据导入数据库,由于json数据太多了,所以希望有一个批量的方法

$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}]'; $array = json_decode($data, true);$values = array();   foreach ($array as $k => $v) {    $values[] = "'" . $v['date'] . "', " . $v['num'] . ", " . $v['area'] . "";}  for ($i=0; $i<count($values); $i++) {    $q = "INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, ".$values[$i].", NOW())";    echo $q.'<br>';}

测试了下,没有问题啊。

INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-06', 151, 18017.24, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-07', 103, 11703.9, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-08', 66, 7378.92, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-09', 192, 22160.96, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-10', 183, 22059.72, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-11', 184, 21212.68, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-12', 119, 13960.93, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-13', 0, 0, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-14', 43, 5056.19, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-15', 121, 12867.43, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-16', 93, 8755.81, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-17', 80, 9035.69, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-18', 158, 17613.12, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-19', 112, 12180.37, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-20', 74, 8614.02, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-21', 95, 10305.27, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-22', 258, 30695.9, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-23', 155, 17457.88, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-24', 148, 16792.02, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-25', 167, 20203.15, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-26', 174, 20055.03, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-27', 138, 15927.18, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-28', 175, 19372.63, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-29', 277, 30226.38, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-30', 311, 33082.01, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-01', 88, 9456.33, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-02', 92, 9384.07, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-03', 70, 7780.39, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-04', 71, 7620.61, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-05', 44, 4672.24, NOW())

方法都是先转为数组,然后foreach写入数据库的。

方法都是先转为数组,然后foreach写入数据库的。



我自己也打印了下,最后提示错误
INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-06', 151, 18017.24, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-07', 103, 11703.9, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-08', 66, 7378.92, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-09', 192, 22160.96, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-10', 183, 22059.72, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-11', 184, 21212.68, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-12', 119, 13960.93, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-13', 0, 0, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-14', 43, 5056.19, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-15', 121, 12867.43, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-16', 93, 8755.81, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-17', 80, 9035.69, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-18', 158, 17613.12, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-19', 112, 12180.37, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-20', 74, 8614.02, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-21', 95, 10305.27, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-22', 258, 30695.9, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-23', 155, 17457.88, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-24', 148, 16792.02, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-25', 167, 20203.15, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-26', 174, 20055.03, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-27', 138, 15927.18, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-28', 175, 19372.63, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-29', 277, 30226.38, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-09-30', 311, 33082.01, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-01', 88, 9456.33, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-02', 92, 9384.07, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-03', 70, 7780.39, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-04', 71, 7620.61, NOW())INSERT INTO f_chengjiao (city_id, fang_time, taoshu, area, os_time) VALUES (1, '2014-10-05', 44, 4672.24, NOW())程序发生错误,请重新添加



我在重新添加了下数据,数据还是到了id=30的时候不添加了,但是提示都添加成功了,到数据库中看数据添加不全
你看下截图

看看你现在的程序

你的json不是只有30?????
怎??有31

    if (mysqli_affected_rows($conn) == 1) {
        echo '这条数据已经添加成功';
    } else {
        echo '程序发生错误,请重新添加';            
    }
??要放在每一次mysqli_query之後呢。

看看你现在的程序



这个是独立的文件,上面2个引入文件是数据库文件,不打紧

你的json不是只有30?????
怎??有31

    if (mysqli_affected_rows($conn) == 1) {
        echo '这条数据已经添加成功';
    } else {
        echo '程序发生错误,请重新添加';            
    }
??要放在每一次mysqli_query之後呢。



前面17条数据是以前写的测试数据,从id=18开始就是导入数据,每次都是到了id=30就结束导入了,这时候数据都没有前面添加完全

你的程序几经修改了,你不给我看到你现在的程序的话,我如何告诉你怎样调试?

你的程序几经修改了,你不给我看到你现在的程序的话,我如何告诉你怎样调试?



抱歉,你看看,这是我最后修改后的
<?php//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":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}]';$array = json_decode($data, true);//print_r ($array);$values = array();  foreach ($array as $k => $v) {	//echo "('" . $k . "', '" . $v . "')";	//echo $v."<br>";	//echo $v['date'].$v['num'].$v['area']."<br>";	$values[] = "'" . $v['date'] . "', " . $v['num'] . ", " . $v['area'] . "";} //echo count($values);for ($i=0; $i<count($values); $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<br />MySQL Error: " . mysqli_error($conn));	//echo $q.'<br>';	if (mysqli_affected_rows($conn) == 1) {		echo '这条数据已经添加成功';	} else {		echo '程序发生错误,请重新添加'; 				}}?>

 echo '程序发生错误,请重新添加'  . $q;

 echo '程序发生错误,请重新添加'  . $q;



没有按照提示报错,而是wamp的报错方式

这就是你的问题了
//require ('inc/config.php');
require (MYSQL);//连接数据库

config 不加载,那 MYSQL 常量是在哪里定义的?

这就是你的问题了
//require ('inc/config.php');
require (MYSQL);//连接数据库

config 不加载,那 MYSQL 常量是在哪里定义的?


config已经打开了
<?phprequire ('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":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}]';$array = json_decode($data, true);//print_r ($array);$values = array();  foreach ($array as $k => $v) {	//echo "('" . $k . "', '" . $v . "')";	//echo $v."<br>";	//echo $v['date'].$v['num'].$v['area']."<br>";	$values[] = "'" . $v['date'] . "', " . $v['num'] . ", " . $v['area'] . "";} //echo count($values);for ($i=0; $i<count($values); $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<br />MySQL Error: " . mysqli_error($conn));	//echo $q.'<br>';	if (mysqli_affected_rows($conn) == 1) {		echo '这条数据已经添加成功';	} else {		echo '程序发生错误,请重新添加'. $q; 				}}?>

虽然添加成功,但是还是到id=30的时候就不添加后面的数据了(数据实际到了9月17号就结束了)

截图中有30个添加成功

你总是再添加,并没有哪里再删除,数据都加了好几遍了
你也不看看?

30个成功我看到了,但是数据添加不全,你可以看看我在11楼的截图
整个数据是id=1到id=17是我以前写程序添加的测试数据,并不在json内,json数据是从id=18开始,添加到id=30就结束了,但是这时候的json在id=30以后的数据数据库里面没有

截图中有30个添加成功

你总是再添加,并没有哪里再删除,数据都加了好几遍了
你也不看看?



我知道那里错了,谢谢提醒,原来代码没有错,是我数据库设置的问题。给分结贴,谢谢大家的耐心解答。

不是还有下一页吗?

不是还有下一页吗?


看到了
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。