Jump to content

Recommended Posts

Basically i have this code:

 

<?php 	
##############################	
######### CONFIG #############	
##############################	
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'series';
mysql_select_db($dbname);	
#############################



$table_name = mysql_real_escape_string('COL1HERECOL1HERECOL1HERE');

        // You can add your links into an array like this 
   $links = array(COL2HERECOL2HERECOL2HERE'); 
    
        $sql = "DROP TABLE IF EXISTS `".$table_name."`";
        mysql_query($sql);
        
        //ONLY USE THIS WHEN EPISODES LIST IS EMPTY
        //$sql = "DROP TABLE IF EXISTS `".$table_name."_episodes`";
        //mysql_query($sql);
        //ONLY USE THIS WHEN EPISODES LIST IS EMPTY
         
        $sql = "CREATE TABLE IF NOT EXISTS `".$table_name."` (
                  `season_id` int(11) NOT NULL,
                  `episode_id` int(11) NOT NULL,
                  `episode_name` varchar(255) NOT NULL,
                  `episode_desc` varchar(2500) NOT NULL
               )";
        mysql_query($sql);
        
        $sql2 = "CREATE TABLE IF NOT EXISTS `".$table_name."_episodes` (
                  `season_id` int(11) NOT NULL,
                  `episode_id` int(11) NOT NULL,
                  `url` varchar(255) NOT NULL,
                  `link_name` varchar(255) NOT NULL,
                  `verified` tinyint(1) NOT NULL DEFAULT '0',
                  `processed` tinyint(1) NOT NULL,
                  UNIQUE KEY `url` (`url`)
                )";
        mysql_query($sql2);        
    
    //Delets all records and changed auto-increment to 1
    mysql_query("DELETE FROM `" . $table_name . "`");
    mysql_query("ALTER TABLE `" . $table_name . "` AUTO_INCREMENT=1");
    
// Or you can add them like this
// $links[] = 'http://www.imdb.com/title/tt0108778/episodes';
// $links[] = 'http://www.imdb.com/title/tt0108778/episodes';
// and so on and so forth

foreach( $links as $link ) {
	$html = get_file_contents($link);
	preg_match_all( '/<table(.*?)>(.*?)<\/table>/is', $html, $results );
	$elements = $results[2];

	foreach( $elements as $html ) {
		if( strstr( $html, 'episode_slate_container' ) || strstr($html, '<h3>Season' ) ) {
			preg_match_all( '/<h3>(.*?)<\/h3>/is', $html, $h3 );
			preg_match( '/([0-9].*?),/is', $h3[1][0], $season);
			preg_match( '/Episode(.*?)<\/a>/is', $h3[1][0], $episode );
			preg_match( '/([0-9].*?):/is', $episode[1], $episode_number );

			$season_number = str_ireplace( ',','', trim( $season[0] ) );
			$episode_name = strip_tags( trim( $episode[0] ) );
			$episode_number = trim( $episode_number[1] );

			preg_match_all( '/<br>(.*?)<\/td>/is', $html, $text );

			//$content = strip_tags( trim( $text[1][0] ) );
			$episode_desc = strip_tags( trim( $text[1][0] ) );

			// Insert the data into the database (Comment out this line and uncomment those others below if you want to debug)
			mysql_query("INSERT INTO " . $table_name . " (season_id,episode_id,episode_name,episode_desc) VALUES('".@mysql_escape_string($season_number)."','".@mysql_escape_string($episode_number)."','".@mysql_escape_string($episode_name)."','".@mysql_escape_string($episode_desc)."') "); 

			// I used the show info for debugging just to make sure the content was extracted correctly you can remove this line if you want
			//$showInfo[] = array( $season_number, $episode_number, $episode_name, $episode_desc );
		}
	}

	// You dont need these two lines but I kept them in just in case you wanted to debug it.
	//print_r($showInfo);
	//exit;
}

function get_file_contents( $url )
{
	if ( !function_exists( 'file_get_contents' ) || ini_get( 'allow_url_fopen' ) == 0 || ini_get( 'allow_url_fopen' ) == 'off' || ini_get( 'allow_url_fopen' ) == '0' ) {
	    $c = curl_init ( );
	    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
	    curl_setopt( $c, CURLOPT_URL, $url );
	    $contents = curl_exec( $c );
	    curl_close( $c );

	    return $contents;
	}
	else {
		$contents = @file_get_contents( $url );

		return $contents;
	}
}
?>

 

i also have a myql database which has value in:

 

$table

$url

 

basically i want to be able to put the above values into line 17 & 20. (where i have spammed COL1HERE & COL2HERE)

 

after the value are put in i want the entire script to run, then i want it to put the nexts db rows values in... until all the rows in the DB have been done.

 

How would i go about this?

not even entirely sure what you are asking, however if looks like you are trying to put db values into an array..? why not use mysql_fetch_array or another mysql_fetch built-in function that the framework provides..

well i have already done something like that:

 


    $get_query = mysql_query("SELECT * FROM `links` WHERE `processed`='0'");
    if ($row = mysql_fetch_array($get_query)){

        $show = $row['show'];
        $url = $row['url'];
        
    	$table_name = mysql_real_escape_string('$show'); // The table name to place the shows into
    	$links = array('$url'); // You can add your links into an array

 

 

but when echoing $links it just returns "array"

 

 

and $links is used further down the script:

 

    	foreach( $links as $link ) {
    
    		$html = get_file_contents($link);

 

where it will throw out errors...

 

Fatal error: Call to undefined function get_file_contents()

1. if you are expecting more then one row to be selected, use a while loop instead of an if statement..

2. you cannot echo an array, as it will output "Array" you need to use a foreach loop to isolate each value/key of an array..

3. do a print_r($links) and post the output..

1. if you are expecting more then one row to be selected, use a while loop instead of an if statement..

2. you cannot echo an array, as it will output "Array" you need to use a foreach loop to isolate each value/key of an array..

3. do a print_r($links) and post the output..

 

1. no i want 1 row at a time to be selected, then at the bottom of the script ill add a refresh function.

 

2. im using the foreach loop...?

 

3. Array ( [0] => $url )

 

(im confused as to why its showing like that on this forum)... ill space it out.

 

Array ( [ 0 ] = > $url )

 

 

Also inside the foreach loop i tried this:

 

echo $links[0];

 

which outputted: $url

removing the quotes did give this output:

 

Array ( [ 0 ] => http://www.urltest.com )

 

btw its file_get_contents not get_file_contents

 

i dont think that correct. as this script works perfectly fine when i just insert the values in the array, (rather than getting from db)

"Fatal error: Cannot redeclare file_get_contents()"

 

which is this:

 

"function file_get_contents($url) "

 

You don't need the function keyword before your call; it's a built in function and php thinks you're trying to re-write it

"Fatal error: Cannot redeclare file_get_contents()"

 

which is this:

 

"function file_get_contents($url) "

 

You don't need the function keyword before your call; it's a built in function and php thinks you're trying to re-write it

 

without the function its errors out saying unexpected "{"

 

putting the function back in actually does make it work however i have a problem that one of the $link in the db is a number... so its not inserting into that table.

I don't know what you are doing but what devilsAdvocate said also is 100% correct. You can't redeclare PHP's built in functions which you are doing if you put the function keyword in fron of it. The error you are getting is indicating that you are having too many opening '{' or you are missing some closing '}' somewhere in your code. Maybe paste your full code here so people can take a look at it. Really sounds like there is a lot of things wrong in your code.

Okay i already have pasted the entire code at the top, however i will paste the new edited code:

 


<?php 

require('connect.php');

    $get_query = mysql_query("SELECT * FROM `links` WHERE `processed`='0'");
    if ($row = mysql_fetch_array($get_query)){

        $show = $row['show'];
        $url = $row['url'];
        
    	$table_name = mysql_real_escape_string($show); // The table name to place the shows into
    	$links = array($url); // You can add your links into an array
    
            $sql = "DROP TABLE IF EXISTS `".$table_name."`";
    
            mysql_query($sql);
    
            
            //ONLY USE THIS WHEN EPISODES LIST IS EMPTY
    
            //$sql = "DROP TABLE IF EXISTS `".$table_name."_episodes`";
    
            //mysql_query($sql);
    
            //ONLY USE THIS WHEN EPISODES LIST IS EMPTY
    
             
    
            $sql = "CREATE TABLE IF NOT EXISTS `".$table_name."` (
    
                      `season_id` int(11) NOT NULL,
    
                      `episode_id` int(11) NOT NULL,
    
                      `episode_name` varchar(255) NOT NULL,
    
                      `episode_desc` varchar(2500) NOT NULL
    
                   )";
    
            mysql_query($sql);
    
            
    
            $sql2 = "CREATE TABLE IF NOT EXISTS `".$table_name."_episodes` (
    
                      `season_id` int(11) NOT NULL,
    
                      `episode_id` int(11) NOT NULL,
    
                      `url` varchar(255) NOT NULL,
    
                      `link_name` varchar(255) NOT NULL,
    
                      `verified` tinyint(1) NOT NULL DEFAULT '0',
    
                      `processed` tinyint(1) NOT NULL,
    
                      UNIQUE KEY `url` (`url`)
    
                    )";
    
            mysql_query($sql2);        
    
        
    
        //Delets all records and changed auto-increment to 1
    
        mysql_query("DELETE FROM `" . $table_name . "`");
    
        mysql_query("ALTER TABLE `" . $table_name . "` AUTO_INCREMENT=1");
    
        
    
    	// Or you can add them like this
    
    	// $links[] = 'http://www.imdb.com/title/tt0108778/episodes';
    
    	// $links[] = 'http://www.imdb.com/title/tt0108778/episodes';
    
    	// and so on and so forth
    
    	
    
    	foreach( $links as $link ) {
        
    		$html = file_get_contents($link);
    
    		preg_match_all( '/<table(.*?)>(.*?)<\/table>/is', $html, $results );
    
    		$elements = $results[2];
    
    
    
    		foreach( $elements as $html ) {
    
    			if( strstr( $html, 'episode_slate_container' ) || strstr($html, '<h3>Season' ) ) {
    
    				preg_match_all( '/<h3>(.*?)<\/h3>/is', $html, $h3 );
    
    				preg_match( '/([0-9].*?),/is', $h3[1][0], $season);
    
    				preg_match( '/Episode(.*?)<\/a>/is', $h3[1][0], $episode );
    
    				preg_match( '/([0-9].*?):/is', $episode[1], $episode_number );
    
    
    
    				$season_number = str_ireplace( ',','', trim( $season[0] ) );
    
    				$episode_name = strip_tags( trim( $episode[0] ) );
    
    				$episode_number = trim( $episode_number[1] );
    
    				
    
    				preg_match_all( '/<br>(.*?)<\/td>/is', $html, $text );
    
    				
    
    				//$content = strip_tags( trim( $text[1][0] ) );
    
    				$episode_desc = strip_tags( trim( $text[1][0] ) );
    
    				
    
    				// Insert the data into the database (Comment out this line and uncomment those others below if you want to debug)
    
    				mysql_query("INSERT INTO " . $table_name . " (season_id,episode_id,episode_name,episode_desc) VALUES('".@mysql_escape_string($season_number)."','".@mysql_escape_string($episode_number)."','".@mysql_escape_string($episode_name)."','".@mysql_escape_string($episode_desc)."') "); 
    
    				
    
    				// I used the show info for debugging just to make sure the content was extracted correctly you can remove this line if you want
    
    				//$showInfo[] = array( $season_number, $episode_number, $episode_name, $episode_desc );
    
    			}
    
    		}
    
    		
    
    		// You dont need these two lines but I kept them in just in case you wanted to debug it.
    
    		//print_r($showInfo);
    
    		//exit;
    
    	}
    
    
    
    	function file_get_contents( $url ){
    
    		if ( !function_exists( 'file_get_contents' ) || ini_get( 'allow_url_fopen' ) == 0 || ini_get( 'allow_url_fopen' ) == 'off' || ini_get( 'allow_url_fopen' ) == '0' ) {
    
    		    $c = curl_init ( );
    
    		    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
    
    		    curl_setopt( $c, CURLOPT_URL, $url );
    
    		    $contents = curl_exec( $c );
    
    		    curl_close( $c );
    
    		
    
    		    return $contents;
    
    		}
    
    		else {
    
    			$contents = @file_get_contents( $url );
    
    			
    
    			return $contents;
    
    		}         
    	}
        mysql_query("UPDATE `imdb_links` SET `processed`=1 WHERE `url`='$url'");
    }

?>

You can't redeclare the PHP file_get_contents function like you do in here.

    	function file_get_contents( $url ){
    
    		if ( !function_exists( 'file_get_contents' ) || ini_get( 'allow_url_fopen' ) == 0 || ini_get( 'allow_url_fopen' ) == 'off' || ini_get( 'allow_url_fopen' ) == '0' ) {
    		    $c = curl_init ( );
    		    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
    		    curl_setopt( $c, CURLOPT_URL, $url );
    		    $contents = curl_exec( $c );
    		    curl_close( $c );
    		    return $contents;
    		}
    		else {
    			$contents = @file_get_contents( $url );
    			return $contents;
    		}         
    	}

 

You need to change the function name to something of your own like function myGetContents.

    	function myGetContents( $url ){
    
    		if ( !function_exists( 'file_get_contents' ) || ini_get( 'allow_url_fopen' ) == 0 || ini_get( 'allow_url_fopen' ) == 'off' || ini_get( 'allow_url_fopen' ) == '0' ) {
    		    $c = curl_init ( );
    		    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
    		    curl_setopt( $c, CURLOPT_URL, $url );
    		    $contents = curl_exec( $c );
    		    curl_close( $c );
    		    return $contents;
    		}
    		else {
    			$contents = @file_get_contents( $url );
    			return $contents;
    		}         
    	}

 

And the above was only declaring the function to be ready for usage, it does not do anything before you actually use it somewhere. Now you would get contents from your function using the function like this:

$someContent = myGetContent('http://someurl.com/');

 

And this is totally useless condition in your if since it will exists because it is built in the PHP.

!function_exists( 'file_get_contents' ) // <-- not needed

[]

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.