如何关闭从DOM插入MySQL的PHP​​循环 [英] How to close PHP loop that inserts from DOM into MySQL

查看:129
本文介绍了如何关闭从DOM插入MySQL的PHP​​循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题对于stackoverflow可能有点具体,但在这里。我有一个PHP文件,它采用HTML,将其写入一个新文件,将文件名插入数据库...一切正常。

This question may be a bit specific for stackoverflow, but here it is. I have a php file that is taking html, writing it to a new file, inserting the file name into a database...all that works fine.

现在我想要使用DOM提取html中的链接。我从这里获得了代码并获取以下内容错误:

Now I want to extract the links in the html, using DOM. I got code from here and get the following error:

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

Parse error: syntax error, unexpected $end ... on line 72

看起来似乎我忘了关闭某些东西或者毫无疑问地关闭了某些东西。但是,唯一的新代码来自上面的链接,它似乎是有序的。但是我是DOM和PHP的新手,所以也许你可以提供帮助。任何指针都表示赞赏。以下是我添加的内容:

It would seem that I forgot to close something or closed something unsuspectingly. However the only new code is from the above link and it appears to be in order. However I am new to DOM and PHP, so perhaps you can help. Any pointers are appreciated. Here is what I have added:

$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";
}

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

For the sake of completeness, here is the whole code with the new bits included:

<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 循环:

You'll then see that the following for loop :

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

未关闭:没有结尾} 对应于其开头 {

is not closed : there is no ending } that correspond to its opening {

(实际上,消息表明某处缺少} ,这在缩进代码时会显示 - 可能是这个for-loop没有关闭,或者是你的其他 {;由你决定应该关闭什么,取决于你的代码的逻辑)

(Well, actually, the message indicates there is a missing } somewhere, and this is shown when indenting the code -- it might be this for-loop that's not closed, or one of your other { ; up to you to find out what should be closed, depending on your code's logic)



或许问题是你有两个用于循环看起来有点相同:


Or maybe the problem is that you have two for loops that looks kind of the same :

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

所以,要么:


  • 关闭外部 -loop,

  • 或删除它,如果它没用。

  • close the outer for-loop,
  • or remove it, if it's not useful.

这篇关于如何关闭从DOM插入MySQL的PHP​​循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆