[英]Insert multiple SQL Queries while in PHP Loop
我正在獲取數據,而在循環中,嘗試將其插入到mysql表中。 第一次插入(一次很多插入)有效,但是在第一次輸入之后,腳本中此后的每次嘗試都會得到錯誤。
成功:
已添加id
為0的新條目。
錯誤:
mysqli :: query():無法獲取mysqli(從其他幾次查詢嘗試中重復了幾次。)
碼:
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($resultspage);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$data = array();
$rows = $xpath->query('//p[@class="row"]'); // get all rows
foreach($rows as $entries) { // loop each row
$entry = array();
$entry['title'] = $xpath->query('./span[@class="txt"]/span[@class="pl"]/a', $entries)->item(0)->nodeValue;
$entry['link'] = 'http://' . $base_url . $xpath->query('./a[@class="i"]', $entries)->item(0)->getAttribute('href');
$entry['price'] = $xpath->query('./span[@class="txt"]/span[@class="l2"]/span[1]', $entries)->item(0)->nodeValue;
$location = $xpath->query('./span[@class="txt"]/span[@class="l2"]/span[2]', $entries)->item(0)->nodeValue;
$loc = str_replace(array('(', ')'), '', $location);
$entry['location'] = $loc;
$entry['seller'] = $xpath->query('./span[@class="txt"]/span[@class="l2"]/a', $entries)->item(0)->nodeValue;
//Get Address
$url2 = $entry['link'];
$page = file_get_contents($url2);
$dom2 = new DOMDocument();
libxml_use_internal_errors(true);
$dom2->loadHTML($page);
libxml_clear_errors();
$xpath2 = new DOMXpath($dom2);
$mapsection = $xpath2->query('//div[@class="mapAndAttrs"]');
$entry['address'] = $xpath2->query('//div[@class="mapAndAttrs"]/div[@class="mapbox"]/div[@class="mapaddress"]')->item(0)->nodeValue;
//End of Get Address
$text_node = $xpath->query('./span[@class="txt"]/span[@class="l2"]/span[1]/following-sibling::text()[1]', $entries)->item(0)->nodeValue;
// remove "/"" and "-"" | explode by space | filter space (now, its left by 2 values: bedroom and size)
$text_node = array_filter(explode(' ', str_replace(array('/', '-'), '', $text_node)));
$entry['bedrooms'] = array_shift($text_node); // bedroom
$entry['dimensions'] = array_shift($text_node); // dimensions
$data[] = $entry; // after gathering necessary items, assign inside
//put data into db
$q = "INSERT INTO `list` (`title`,`price`, `rooms`, `dimensions`, `location`, `address`, `seller`, `href`) VALUES ('".$entry['title']."','".$entry['price']."', '".$entry['bedrooms']."','".$entry['dimensions']."','".$entry['location']."','".$entry['address']."','".$entry['seller']."','".$entry['link']."')";
if ( $mysqli->query($q) ) {
echo "A new entry has been added with the `id` of {$mysqli->insert_id}.";
} else {
echo "There was a problem:<br />$q<br />{$mysqli->error}";
}
//Close it off
$mysqli->close();
}
echo '<pre>';
print_r($data);
我希望有人能幫助我理解為什么所有這些查詢(在第一個查詢之后)都不成功。 我正在嘗試插入所有查詢。 謝謝你的時間!
您過早關閉mysql連接。 它應該是
$q = "INSERT INTO `list` (`title`,`price`, `rooms`, `dimensions`, `location`, `address`, `seller`, `href`) VALUES ('".$entry['title']."','".$entry['price']."', '".$entry['bedrooms']."','".$entry['dimensions']."','".$entry['location']."','".$entry['address']."','".$entry['seller']."','".$entry['link']."')";
if ( $mysqli->query($q) ) {
echo "A new entry has been added with the `id` of {$mysqli->insert_id}.";
} else {
echo "There was a problem:<br />$q<br />{$mysqli->error}";
}
}
//Close it off
$mysqli->close();
echo '<pre>';
print_r($data);
代替。
注意
正確縮進代碼對於快速發現此類錯誤非常有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.