seany123 Posted August 4, 2011 Share Posted August 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/ Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251785 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 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() Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251872 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251877 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251884 Share on other sites More sharing options...
TOA Posted August 4, 2011 Share Posted August 4, 2011 What version of php do you have? I ask because of this error: Fatal error: Call to undefined function get_file_contents() Usually that's because the version is less than 5.x Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251892 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 What version of php do you have? I ask because of this error: Fatal error: Call to undefined function get_file_contents() Usually that's because the version is less than 5.x currently running: PHP Version 5.3.5 Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251895 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 when creating your array, do not wrap the variable in quotes.. $links = array($url); then print_r again Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251896 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 btw its file_get_contents not get_file_contents Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251897 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251901 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 It is correct what AyKay47 said. You are using wrong name for the function. Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251906 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 okay i have changed them all... but im getting this error: "Fatal error: Cannot redeclare file_get_contents()" which is this: "function file_get_contents($url) " Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251916 Share on other sites More sharing options...
TOA Posted August 4, 2011 Share Posted August 4, 2011 "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 Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251933 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 "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. Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251939 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251948 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 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'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251952 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 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 [] Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1251957 Share on other sites More sharing options...
seany123 Posted August 4, 2011 Author Share Posted August 4, 2011 Using what you have given me i have made the script work with the database. the only problem i have now is that when $show is a number EG: 24 Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1252033 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2011 Share Posted August 6, 2011 What problem you exactly encounter when the show is number? Quote Link to comment https://forums.phpfreaks.com/topic/243810-help-getting-data-from-db-and-putting-into-array-needed/#findComment-1253174 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.