开发者

How to close PHP loop that inserts from DOM into MySQL

开发者 https://www.devze.com 2023-02-17 21:24 出处:网络
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开发者_如何学Python the file name into a database...all

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开发者_如何学Python the file name into a database...all that works fine.

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

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

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>


Try indenting your code properly.

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 {

(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

So, either :

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


You must have not close for loop here

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

精彩评论

暂无评论...
验证码 换一张
取 消