dongmi5020 2014-08-06 16:39
浏览 65
已采纳

在PHP循环中插入多个SQL查询

I am getting data and while in a loop, trying to insert it into a mysql table. The first insert (of many at a time) works, but after the first entry, I get the error for every attempt thereafter in the script.

Success:

A new entry has been added with the id of 0.

Error:

mysqli::query(): Couldn't fetch mysqli (Which gets repeated several times, from several other query attempts.)

Code:

$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);

I would like somebody to assist me with understanding why all of these queries (after the first one) are unsuccessful. I am trying to get all queries to insert. Thanks for your time!

  • 写回答

1条回答 默认 最新

  • dongmei8209 2014-08-06 16:47
    关注

    You're closing your mysql connection a bit too early. It should be

    $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);
    

    instead.

    Note

    Proper indenting of code is a great help to see such mistakes quickly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示