multiple words for search engines

Discussion in 'PHP' started by Katie_P, Mar 13, 2007.

  1. #1
    Hi people sorry to bother you, I'm kinda new to php and I'm struggling to edit this code so that is searches for multiple words.

    I understand you need to use the something along the lines of this
    -----------------------------------------------------------------
    $sql = "SELECT whatever, more, stuff FROM table WHERE ";

    $keywordArray = explode(" ", $_POST['keyword']);

    foreach ($keywordArray As $val) {
    $sql .= "(keyword LIKE '".$val."') ";
    }


    //the SQL is either doing an AND or OR search by replacing the )( that are rendered into ) AND ( or ) OR (//

    $sql = str_replace(") (", ") AND (", $sql);

    $sql .= "LIMIT 25;";

    -----------------------------------------------------------------

    i simply keep getting errors when im trying to insert the above code into the one below. please can some hep with the edit of these code. Thanks


    <?
    print "<html><head><title>My Search Engine</title></head><body>n";

    if( $_POST['keyword'] )

    {

    include("dbconnect.php");

    /* Get timestamp before executing the query: */

    $start_time = getmicrotime();

    /* Set $keyword and $results, and use addslashes() to

    * minimize the risk of executing unwanted SQL commands: */

    $keyword = addslashes( $_POST['keyword'] );

    $results = addslashes( $_POST['results'] );

    /* Execute the query that performs the actual search in the DB: */

    $result = mysql_query(" 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" );


    /* Get timestamp when the query is finished: */

    $end_time = getmicrotime();



    $number = mysql_num_rows($result);

    if ($number != "0") {
    /* Present the search-results: */

    print "<h2>Search results for '".$_POST['keyword']."':</h2>n";

    for( $i = 1; $row = mysql_fetch_array($result); $i++ )

    {

    print "$i. <a href='".$row['url']."'>".$row['url']."[/url]n";

    print "(Index count: ".$row['occurrences'].")

    n";

    }

    } else {
    print "<p>No matching results found/p>";
    }

    /* Present how long it took the execute the query: */

    print "query executed in ".(substr($end_time-$start_time,0,5))." seconds.";

    }

    else

    {

    /* If no keyword is defined, present the search page instead: */

    }

    print "</body></html>n";


    /* Simple function for retrieving the current timestamp in microseconds: */

    function getmicrotime()

    {

    list($usec, $sec) = explode(" ",microtime());

    return ((float)$usec + (float)$sec);

    }

    ?>
     
    Katie_P, Mar 13, 2007 IP
  2. sadcox66

    sadcox66 Spirit Walker

    Messages:
    496
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you post your error message
     
    sadcox66, Mar 13, 2007 IP