garryBrown Posted April 14, 2013 Share Posted April 14, 2013 Hello, I've written a code that menat to do the following:I've got the form where users enters an URL link and a keyword. The website should be accessed and occurence of the keyword is counted and then the result is sent to a SQLite database. But the problem is the following:every time I enter URL link, I receive a message:Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream: No such file or directory in E:\wwwroot\add.php on line 33Could not open URL!Any ideas on how to fix this bug? Thanks in advance. <?php //Open connection with a database $db=sqlite_open("search.db", 0666, $sqliteerror); $URL = $_POST['URL']; $keyword = $_POST['keyword']; //If no URL entered: if( !$URL ) { die( "You need to define a URL to process." ); } //Check whether entered URL is already recorded: $handle = sqlite_query($db,"SELECT page_id FROM page WHERE page_url = \"$URL\""); $row = sqlite_fetch_array($handle, SQLITE_ASSOC ); if( $row['page_id'] ) { //if yes, use old page_id: $page_id = $row['page_id']; } else { //If not, a new line containing URL is created: sqlite_query($db,'INSERT INTO page (page_url) VALUES ("$URL")'); $page_id = sqlite_last_insert_rowid($db); } //Open URL contents and start parsing through the text for creating index in a database: if( !($fd = file_get_contents($URL,"r"))) die( "Could not open URL!" ); while( $buf = fgets($fd,1024) ) { //Extract all words matching the regexp from the current line: preg_match_all("/(\b[\w+]+\b)/",$buf,$words); // Loop through all words/occurrences and insert them into the database: for( $i = 0; $words[$i]; $i++ ) { for( $j = 0; $words[$i][$j]; $j++ ) { //Checking whether a current word already has a record in the database: $cur_word = addslashes( strtolower($words[$i][$j]) ); $handle = sqlite_query($db,"SELECT word_id FROM word WHERE word_word = '$cur_word'"); $row = sqlite_fetch_array($handle, SQLITE_ASSOC ); if( $row['word_id'] ) { //if yes, the old word id is used: $word_id = $row['word_id']; } else { //if not, a new word is created: sqlite_query($db,'INSERT INTO word (word_word) VALUES (\"$cur_word\")'); $page_id = sqlite_last_insert_rowid($db); } //Registering the occurence of the word into a database: sqlite_query($db,'INSERT INTO occurrence (word_id,page_id) VALUES ($word_id,$page_id)'); } } } fclose($fd); //Searching the database of URL, keywords and occurencies of keywords: $handle = sqlite_query($db," SELECT p.page_url AS url, COUNT(*) AS occurrences FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.word_id = o.word_id AND w.word_word = \"$keyword\" GROUP BY p.page_id ORDER BY occurrences DESC LIMIT $results" ); //Display of search results: print "<h2>Search results for '".$_POST['keyword']."':</h2>\n"; for( $i = 1; $row = sqlite_fetch_array($handle); $i++ ) { print "$i. <a href='".$row['URL']."'>".$row['URL']."</a>\n"; print "(occurrences: ".$row['occurrences'].")<br><br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276947-php-sqlite/ Share on other sites More sharing options...
jcbones Posted April 14, 2013 Share Posted April 14, 2013 file_get_contents() can open addresses only if the fopen wrappers are enabled in the ini. So check that. Also you would have to pass the protocol to the function, otherwise it will think you are looking for a file. Which is most likely to be your problem. So it should be http://www.google.com Quote Link to comment https://forums.phpfreaks.com/topic/276947-php-sqlite/#findComment-1424728 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.