将循环数据插入sql

时间:2012-11-22 21:35:55

标签: sql xpath

我正在尝试用xpath抓取一些数据然后插入SQL。

Echo工作正常但是当插入sql时,它只获得第一行。

示例:

    <?php 
    $res=mysql_query("SELECT table.title, table.link FROM table WHERE table.link LIKE 'http://www.%' AND streams.title = ''");
    while($row=mysql_fetch_array($res))
    {
      $html = new DOMDocument();
      @$html->loadHtmlFile($row["link"]);
      $xpath = new DOMXPath($html);

      foreach ($xpath->query("//table[@style='margin-left: 5px;']//td[@width='150']//a") as $files)
      {
        $html = new DOMDocument();
        @$html->loadHtmlFile($files->getAttribute('href'));
        $xpath = new DOMXPath($html);

        $hl = $xpath->query("//div[@style='width:742px']/a[preceding-sibling::div[@id='help1']]/@href")->item(0)->nodeValue;
        $link=serialize(array('link' => $hl));
        $link=sqlesc($link); 
        echo $link; // all lines gets printed, thats okay?
        mysql_query("UPDATE streams SET streams.hl=$link") or die(mysql_error()); // Only first line gets inserted, why?
      }

    }  
    ?>

1 个答案:

答案 0 :(得分:0)

您正在更新所有行,并且您获得包含相同数据的所有行,因为上次更新会更新所有行,您要更新现有行还是只插入新行?

如果你想插入试试这个

INSERT INTO streams (hl) VALUES ($link)

如果表中还有其他非可选列,请同时插入这些值