php mysql insert into while 意外終止
PHP code
$result = mysql_query("SELECT uid , pid , cate FROM upcm");while($rows = mysql_fetch_row($result)){$arry = explode($sepr,$rows[2],5);$query = "INSERT INTO upcn(uid,pid,cate1,cate2,cate3,cate4,cate5) VALUES('$rows[0]','$rows[1]','$arry[0]','$arry[1]','$arry[2]','$arry[3]','$arry[4]')";$result1 = mysql_query($query);if(!$result1){echo "fail
";}}
mysql_fetch_row($result)擷取查詢的結果,逐條處理,處理後寫進一個新的表裡面,但是每次執行while迴圈總是還沒執行完程式就終止了,沒有處理完資料,把insert into 換成printf(“**”);代替插入操作,會將程式正確執行完畢,而且每次運行程式插入的條數不一,有時多有時少,請問大俠們什麼情況這是,苦惱啊..
------解決方案--------------------
。。
像你這種問題,肯定是要在cli模式下跑單條sql處理才靠譜啊!!!
至不濟也要先把資料匯出,然後匯入,而不是這樣做啊
參見select into
------解決方案--------------------
有幾個問題需要注意排除:
1、php逾時
2、web伺服器逾時
3、特殊字元未轉義
4、count($array) < 5
演算法上可考慮:
每千條組裝成多個VALUE的INSERT語句後插入
以分頁方式逐段插入
直接使用SQL指令完成,而不經php轉手
$sql =<<< SQL
INSERT INTO upcn(uid,pid,cate1,cate2,cate3,cate4,cate5)
SELECT uid , pid
, substring_index(substring_index(cate,'$sepr',1),'$sepr',-1)
, substring_index(substring_index(cate,'$sepr',2),'$sepr',-1)
, substring_index(substring_index(cate,'$sepr',3),'$sepr',-1)
, substring_index(substring_index(cate,'$sepr',4),'$sepr',-1)
, substring_index(substring_index(cate,'$sepr',5),'$sepr',-1)
FROM upcm
SQL;