【问题标题】:How to close PHP loop that inserts from DOM into MySQL如何关闭从 DOM 插入 MySQL 的 PHP 循环
【发布时间】:2011-03-21 05:30:58
【问题描述】:

这个问题对于 stackoverflow 可能有点特殊,但在这里。我有一个使用 html 的 php 文件,将其写入一个新文件,将文件名插入到数据库中……一切正常。

现在我想使用 DOM 提取 html 中的链接。我从here 得到代码并得到以下错误:

解析错误:语法错误,第 72 行的意外 $end ...

似乎我忘了关闭某些东西或毫无防备地关闭了某些东西。然而,唯一的新代码来自上面的链接,它似乎是有序的。但是我是 DOM 和 PHP 的新手,所以也许你可以帮忙。任何指针表示赞赏。这是我添加的内容:

$dom = new DOMDocument();
@$dom->loadHTML($curl_scraped_page);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

//the above works fine, but when I add the loop bellow it fails


for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    storeLink($url,$target_url);

function storeLink($url) {
    $query = "INSERT INTO happyturtle (ad2, ad3) VALUES ('$url', '$gathered_from')";
    mysql_query($query) or die('Error, insert query failed');

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
}

为了完整起见,这里是包含新位的整个代码:

<html>
<body>

<?
$urls=explode("\n", $_POST['url']);
$proxies=explode("\n", $_POST['proxy']);

for ( $counter = 0; $counter <= 6; $counter++) {
for ( $count = 0; $count <= 6; $count++) {

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$urls[$counter]);
 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
 curl_setopt($ch, CURLOPT_PROXY,$proxies[$count]);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
 curl_setopt ($ch, CURLOPT_HEADER, 1); 
curl_exec ($ch); 
$curl_scraped_page = curl_exec($ch); 

$FileName = rand(0,100000000000);
$FileHandle = fopen($FileName, 'w') or die("can't open file");
fwrite($FileHandle, $curl_scraped_page);


$dom = new DOMDocument();
@$dom->loadHTML($curl_scraped_page);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

$hostname="****";
$username="****";
$password="****";
$dbname="****";
$usertable="****";

$con=mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname ,$con);


for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    storeLink($url,$target_url);

function storeLink($url) {
    $query = "INSERT INTO happyturtle (ad2, ad3) VALUES ('$url', '$gathered_from')";
    mysql_query($query) or die('Error, insert query failed');
$sql="INSERT INTO happyturtle (time, ad1)
VALUES
('$FileName','$domains')";

}

mysql_close($con);

fclose($FileHandle);

curl_close($ch);

echo $FileName; 

echo "<br/>";

}
}

?>

</body>
</html>

【问题讨论】:

  • 是的,错误意味着您忘记在某处关闭某些内容。数一数你的括号。正确缩进你的代码,这样可以更容易地发现这些东西。我会自发猜想你在开始时打开了太多 for 循环。
  • 如您所说,新问题与上一个问题完全无关,因此请接受下面的答案并为新问题打开一个新问题。谢谢。

标签: php mysql http dom loops


【解决方案1】:

尝试正确缩进代码。

然后您将看到以下 for 循环:

for ( $counter = 0; $counter <= 6; $counter++) {

没有关闭:没有结尾}与其开头{相对应

(实际上,该消息表明某处缺少},这会在缩进代码时显示——可能是这个for循环没有关闭,或者你的另一个@ 987654326@ ;取决于您的代码逻辑,由您决定应该关闭什么)


或者问题可能是你有两个看起来差不多的for 循环:

for ( $counter = 0; $counter <= 6; $counter++) {
    for ( $count = 0; $count <= 6; $count++) {

        // Some code

    } // closing of the inner for-loop
// Here, the first for-loop is not closed

所以,要么:

  • 关闭外部for-loop,
  • 或删除它,如果它没有用。

【讨论】:

    【解决方案2】:

    这里一定没有关闭for循环

    for ($i = 0; $i < $hrefs->length; $i++) {
        $href = $hrefs->item($i);
        $url = $href->getAttribute('href');
        storeLink($url,$target_url);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2018-07-03
      • 2012-06-19
      • 1970-01-01
      • 2012-07-10
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多