Phlex Posted July 31, 2010 Share Posted July 31, 2010 I'm writing a script to grab some info from a list of urls in the database, but it just continuously loads and doesn't display anything. Here's the script: <? $username="*****"; $password="*****"; $database="*****"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM dvds"; $result=mysql_query($query); $num=mysql_numrows($result); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $url=mysql_result($result,$i,"imdb_url"); echo "<b>Getting page: $url</b><br>"; //get the page content $imdb_content = get_data($url); //parse for movie details $name = get_match('/<title>(.*)<\/title>/isU',$imdb_content); $director = strip_tags(get_match('/<h5[^>]*>Director:<\/h5>(.*)<\/div>/isU',$imdb_content)); $plot = get_match('/<h5[^>]*>Plot:<\/h5>(.*)<\/div>/isU',$imdb_content); $release_date = get_match('/<h5[^>]*>Release Date:<\/h5>(.*)<\/div>/isU',$imdb_content); $mpaa = get_match('/<a href="\/mpaa">MPAA<\/a>:<\/h5>(.*)<\/div>/isU',$imdb_content); $run_time = get_match('/Runtime:<\/h5>(.*)<\/div>/isU',$imdb_content); //build content $content.= '<h2>Film</h2><p>'.$name.'</p>'; $content.= '<h2>Director</h2><p>'.$director.'</p>'; $content.= '<h2>Plot</h2><p>'.substr($plot,0,strpos($plot,'<a')).'</p>'; $content.= '<h2>Release Date</h2><p>'.substr($release_date,0,strpos($release_date,'<a')).'</p>'; $content.= '<h2>MPAA</h2><p>'.$mpaa.'</p>'; $content.= '<h2>Run Time</h2><p>'.$run_time.'</p>'; $content.= '<h2>Full Details</h2><p><a href="'.$url.'" rel="nofollow">'.$url.'</a></p>'; //display the content echo $content; $i++; } //gets the match content function get_match($regex,$content) { preg_match($regex,$content,$matches); return $matches[1]; } //gets the data from a URL function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } ?> Hopefully someone better at php can tell me how to fix it! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/ Share on other sites More sharing options...
trq Posted July 31, 2010 Share Posted July 31, 2010 Its likely your query is failing but your not checking to see. Most of your code should be within an if statement.... if ($result=mysql_query($query)) { // more code } else { trigger_error(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/#findComment-1093491 Share on other sites More sharing options...
Phlex Posted July 31, 2010 Author Share Posted July 31, 2010 Thanks for your reply. The query isn't the problem though. as I tested that part of the code in this previous version of the script: <? $username="*****"; $password="*****"; $database="*****"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM dvds"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $url=mysql_result($result,$i,"imdb_url"); echo "<b>Getting page: $url</b><br>"; $i++; } ?> That works fine. It's when it gets to //get the page content $imdb_content = get_data($url); is where it gets stuck. Something in that get_data() function is not well, functioning...lol...it's not grabbing the page like it should... Quote Link to comment https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/#findComment-1093493 Share on other sites More sharing options...
jcbones Posted July 31, 2010 Share Posted July 31, 2010 I would set the timeout to 0. This would wait until you get a response. After you are sure you're getting a response, then adjust the timeout. Quote Link to comment https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/#findComment-1093554 Share on other sites More sharing options...
Phlex Posted July 31, 2010 Author Share Posted July 31, 2010 Duh! Amazingly the timeout change to 0 worked...The script takes a while to run I guess (slow server/connection?) and I wasn't allowing it to sit and gather the info long enough it seems...Haha...But it does work...Thank you jcbones for the suggestion! =) Quote Link to comment https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/#findComment-1093610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.