我想查詢大量數據到服務器,這裏是我的代碼:MySQL查詢執行失敗
$queryString = "";
$connect = mysqli_connect("localhost", "username", "password", "database");
$loopLength = 20;
$currentGroup = 1;
$currentLoopAmount = 0;
$queryAmount = 5;
for($v = 0; $v < ceil($loopLength/$queryAmount); $v++){
//echo "Looping Main: " . $v . "<br>";
//echo $loopLength - (($currentGroup - 1) * 10) . "<br>";
if($loopLength - (($currentGroup - 1) * $queryAmount) >= $queryAmount){
$currentLoopAmount = $queryAmount;
}
else{
$currentLoopAmount = $loopLength - (($currentGroup - 1) * $queryAmount);
}
//echo $currentLoopAmount;
$queryString = "";
for($q = (($currentGroup - 1) * $queryAmount); $q < $currentLoopAmount + (($currentGroup - 1) * $queryAmount); $q++){
//echo " Looping Sub: " . $q . "<br>";
$tempVariable = grabPageData($URLs[$q], $q);
$queryString .= $tempVariable;
if($q < $loopLength-1){
$queryString .= ",";
}
else{
$queryString .= ";";
}
}
echo $queryString;
$query = "INSERT INTO PublicNoticesTable (url, county, paperco, date, notice, id) VALUES " . $queryString;
$result = mysqli_query($connect, $query);
if($result){
echo "Success";
}
else{
echo "Failed : " . mysqli_error($connect) . "<br>";
}
$currentGroup += 1;
}
的$loopLength
變量是動態的,可以在數千或潛在十萬。我設計了這個功能,將這個龐大的數字劃分爲一批較小的查詢,因爲我無法一次性通過GoDaddy在我的共享託管服務上傳所有數據。變量$queryAmount
代表較小的查詢有多大。
下面是插入到表中的其中一個值集的示例: 這是公告中的數據,表示我的代碼在grabPageData()
函數中檢索到。
('http://www.publicnoticeads.com/az/search/view.asp?T=PN&id=37/7292017_24266919.htm','Pima','Green Valley News and Sun','2017/07/30',' ___________________________ARIZONA SUPERIOR COURT, PIMA COUNTYIn the Matter of the Estate of:JOSEPH T, DILLARD, SR.,Deceased.DOB: 10/09/1931No. PB20170865NOTICE TO CREDITORS(FOR PUBLICATION)NOTICE IS HEREBY GIVEN that DANA ANN DILLARD CALL has been appointed Personal Representative of this Estate. All persons having claims against the Estate are required to present their claimswithin four months after the date of the firat publication of this notice or the claims will be forever barred. Claims must be presented by delivering or mailing a written statement of the claim to the Personal Representative at the Law Offices of Michael W. Murray, 257 North Stone Avenue, Tucson, Arizona 85701.DATED this 17th day of July, 2017./S/ Micahel W. MurrayAttorney for the Personal RepresentativePub: Green Valley News & SunDate: July 23, 30, August 6, 2017 Public Notice ID: 24266919',' 24266919'),
爲了獲得這些數據,我通過抓取頁面並抓取它的函數來運行它。然後我通過這個功能把網頁html代碼:
function cleanData($data){
$data = strip_tags($data);
//$data = preg_replace("/[^a-zA-Z0-9]/", "", $data);
//$data = mysql_real_escape_string($data);
return $data;
}
這給了我沒有標籤的內容,如你所見。這是問題。
該函數執行,一切似乎只是丹迪。然後(我不保持在10題的緣故取決於$queryAmount
變量)的輸出,你可以看到它會在功能類似的功能...
Failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
怪異的是,當我有大量的數據,比如說$loopLength
變量就像116.結果會輸出,「失敗:(錯誤)失敗:(錯誤)輝...(錯誤)成功所以它只是實際查詢最後一組數據???不確定
我不知道如何解決這個問題,我想要一個新鮮的眼睛,有人可以幫助我,我一直在努力解決這個問題...幾個小時,試圖找到解決方案
對不起,讓這個問題的一個痛苦的對接:(
編輯:
我改變了代碼從以前用mysql準備語句並沒有什麼...見下文:
$grabDataResults = [
"url" => "",
"county" => "",
"paperco" => "",
"date" => "",
"notice" => "",
"id" => "",
];
$connect = mysqli_connect("localhost", "bwt_admin", "Thebeast1398", "NewCoDatabase");
if($stmt = mysqli_prepare($connect, "INSERT INTO PublicNoticesTable (url, county, paperco, date, notice, id) VALUES (?, ?, ?, ?, ?, ?)")){
mysqli_stmt_bind_param($stmt, 'ssssss', $grabDataResults["url"], $grabDataResults["county"], $grabDataResults["paperco"], $grabDataResults["date"], $grabDataResults["notice"], $grabDataResults["id"]);
$loopLength = 1;
for($v = 0; $v < $loopLength; $v++){
$grabDataResults = grabPageData($URLs[$v], $v);
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
printf("Error:\n", mysqli_stmt_error($stmt));
echo "(" . $grabDataResults["url"] . "," . $grabDataResults["county"] . "," . $grabDataResults["paperco"] . "," . $grabDataResults["date"] . "," . $grabDataResults["notice"] . "," . $grabDataResults["id"] . ")";
}
mysqli_stmt_close($stmt);
mysqli_close($connect);
}
不幸的是,這是我從輸出獲得:
1 Row inserted. 0 Error:
沒有錯誤實際打印出來,該行是在serted。但是,當我導航到我的數據庫,並查看已存儲的值..他們都是空的。 echo語句輸出這樣的:
(http://www.publicnoticeads.com/az/search/view.asp?T=PN&id=31/7292017_24266963.htm,Yuma,Sun (Yuma), The,2017/07/30,, 24266963)
所以我知道所有變量包含除$notice
變量,它得到由我cleanData()
功能出於某種原因被破壞的東西。
FRO對目前爲止的問題的評論 - 我肯定會使用準備好的語句並綁定這些值。如果您的數據來自網頁,那麼如果直接插入數據會導致您的SQL發生中斷的可能性非常高,https://stackoverflow.com/questions/9629328/how-to-use-mysqli-prepared-statements-in -php可能會有幫助。 –
我做了一些關於創建預處理語句的研究,併爲了不直接插入而對值進行了綁定......看起來像是我的代碼試圖動態生成查詢語句和東西將是一個巨大的混亂。我想我可以嘗試一下,但就像我說過的,我可能會產生成千上萬的數據行,不知道綁定語句會是什麼樣子,更不用說查詢本身了。 – Pixelknight1398
您可以使用1行插入來準備語句,然後每行數據只需執行準備好的語句和一行數據。 –