gpt4 book ai didi

php - 如何关闭从 DOM 插入 MySQL 的 PHP 循环

转载 作者:行者123 更新时间:2023-11-29 14:52:55 25 4
gpt4 key购买 nike

这个问题对于 stackoverflow 来说可能有点具体,但就是这样。我有一个 php 文件,它采用 html,将其写入新文件,将文件名插入数据库......所有这些都工作正常。

现在我想使用 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 循环:

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

未关闭:没有与其开头 {

相对应的结尾 }

(嗯,实际上,该消息表明某处缺少 },并且在缩进代码时会显示这一点 - 可能是这个 for 循环未关闭,或者您的其他 { 之一;由您决定应该关闭哪些内容,具体取决于您的代码逻辑)


或者问题可能是您有两个看起来相似的 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循环,
  • 如果没有用,则将其删除。

关于php - 如何关闭从 DOM 插入 MySQL 的 PHP 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5374160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com