www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP + SQLite

  1. #1
    Join Date
    Jan 2013
    Posts
    20

    PHP + SQLite

    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.

    Code:
    <?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";
       }
    						   
    ?>

  2. #2
    Join Date
    Apr 2013
    Posts
    56
    Try adding http:// infront of your url. You are passing in invalid url address and because of it, you get an error response that such file or directory does not exist.

  3. #3
    Join Date
    Jan 2013
    Posts
    20
    Well I actually type in http://www.google.com every time. And I get the mentioned error message.

  4. #4
    Join Date
    Apr 2013
    Posts
    56
    Try to determine if your url really is correct by printing it out just before you open file stream. Because if you use file_get_contents function with http://www.google.com it will work.

  5. #5
    Join Date
    Jan 2013
    Posts
    20
    Did that as well, still not working. Maybe the problem is in system permissions? I am using windows 7.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles