Jump to content

PHP + SQLite


garryBrown

Recommended Posts

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 33
Could 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";
}
                        
?>
Link to comment
Share on other sites

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.