Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/209429-script-hanging-not-displaying-output/
Share on other sites

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...

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! =)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.