在循环内关闭插入查询

时间:2015-04-21 21:19:58

标签: php mysql

我正在尝试循环插入查询,但是我一直收到错误,这似乎与我的close()语句有关?

我有以下代码,它会在以下错误后继续返回:

Fatal error: Call to a member function close() on a non-object in

我故意删除了insert_news查询中的信息$变量,以简化问题

function check_text($text, $last_id) {
    global $ids;
    foreach($ids as $id => $teams) {
        foreach($teams as $team) {
            if(stripos(($text, $team) !== false) {

            $teamRelation = $con->prepare("INSERT INTO contain (`team_id`, `news_id`) VALUES (?, ?)");
            $teamRelation->bind_param("ii", $id, $last_id);
            $teamRelation->execute();

            }
         }
    }

    $teamRelation->close();

}

function scrape_afton() {

foreach($afton->find("//section/ul/li") as $afton_element) {

            $insert_news = $con->prepare("INSERT INTO news (`title`, `url`, `image_url`, `date`, `news_text`, `website_id`) VALUES (?, ?, ?, ?, ?, 1)");
            $insert_news->bind_param("sssss", $afton_title, $afton_link, $afton_img, $afton_datetime, $full_text_dont);
            $insert_news->execute();
            $afton_last_id = $con->insert_id;

            check_text($afton_full_text, $afton_last_id);

}
}

1 个答案:

答案 0 :(得分:1)

$ con未在函数中定义。我假设它是全局的,所以将它添加到你的全局中:

全球$ ids,$ con;