Jump to content

Could Someone Please Tell Me Why This Script is Looping Infinitely??


Modernvox

Recommended Posts

I have been working on this for 2 weeks already with help from some my  PhpFreak Brothers, but damn it! This script won't stop loading now and I'm running out of perks :P

 

<?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
   $sets[] = fetch_email($link);
}

foreach($sets as $set) {
   $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


if (!$dbx)
{
die('Could not connect: ' . mysql_error());
}

mysql_SELECT_db("craigslist", $dbx);
mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$set')");

mysql_close($dbx);


echo "Link = " . $set['link'] . ", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   $page = file_get_contents($page_url);
   preg_match_all($pattern, $page, $matches);
   return $matches[1];
}

function fetch_email($page_link) {
   $pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
   $page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
   preg_match($pattern, $page, $out);
   return array('link'=>$page_link, 'email'=>$out[1]);
}

?>

well the first thing I notice is that you are opening and closing the connections way too much. You should put that outside of the foreach loop. Try this:

 

<?php
//connect to database
$dbx= mysql_connect("localhost", "root", "") or die(mysql_error());

//select the database
mysql_select_db("craigslist") or die(mysql_error());

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
     $sets[] = fetch_email($link);
}

foreach($sets as $set) {
     //make the query
     mysql_query("INSERT INTO addresses (sale_items) VALUES ('$set')") or die(mysql_error());

     echo "Link = " . $set['link'] . ", E-mail = " . $set['email'] . "<br/>";
}

//close the connection
mysql_close($dbx);

function fetch_links($page_url) {
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   $page = file_get_contents($page_url);
   preg_match_all($pattern, $page, $matches);
   return $matches[1];
}

function fetch_email($page_link) {
   $pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
   $page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
   preg_match($pattern, $page, $out);
   return array('link'=>$page_link, 'email'=>$out[1]);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.