Modernvox Posted November 3, 2009 Share Posted November 3, 2009 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 <?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]); } ?> Link to comment https://forums.phpfreaks.com/topic/180066-could-someone-please-tell-me-why-this-script-is-looping-infinitely/ Share on other sites More sharing options...
ngreenwood6 Posted November 3, 2009 Share Posted November 3, 2009 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]); } ?> Link to comment https://forums.phpfreaks.com/topic/180066-could-someone-please-tell-me-why-this-script-is-looping-infinitely/#findComment-949967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.