【问题标题】:Insert multiple SQL Queries while in PHP Loop在 PHP 循环中插入多个 SQL 查询
【发布时间】:2014-09-29 16:48:09
【问题描述】:

我正在获取数据并在循环中尝试将其插入到 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);

我希望有人能帮助我理解为什么所有这些查询(在第一个查询之后)都不成功。我正在尝试插入所有查询。感谢您的宝贵时间!

【问题讨论】:

  • 你在用mysqli,太好了!如果您利用准备好的语句并将输入值绑定到参数,那就更好了。您只需在循环之前准备一次查询。
  • 我没看错,你在 foreach 循环中关闭了你的 mysqli 连接。在外面做。

标签: php mysql xpath mysqli foreach


【解决方案1】:

您关闭 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);

改为。

注意

正确缩进代码有助于快速发现此类错误。

【讨论】:

  • 这是一个愚蠢的错误。谢谢你接听。
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多