Search Multiple Keywords Not one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
willevans1
Forum Newbie
Posts: 4
Joined: Tue Sep 21, 2010 3:21 pm

Search Multiple Keywords Not one

Post by willevans1 »

Ok, I have the code below and it only searches one keyword of my database please could you tell me how I can search multiple keywords?

Code: Select all

<?
/*
* search.php
*
* Script for searching a datbase populated with keywords by the
* populate.php-script.
*/
print "<html><head><title>[Squashy] Search! NOT MESSED UP.</title></head><body>\n";
if( $_POST['keyword'] )
{
    /* Connect to the database: */
    mysql_pconnect("mysql3.000webhost.com","a4580813_dba","censored")
        or die("ERROR: Could not connect to database!");
    mysql_select_db("a4580813_db");
    /* Get timestamp before executing the query: */
    $start_time = getmicrotime();
    /* 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 = \"".$_POST['keyword']."\"
                            GROUP BY
                                p.page_id
                            ORDER BY
                                occurrences DESC
                            LIMIT ".$_POST['results'] );
    /* Get timestamp when the query is finished: */
    $end_time = getmicrotime();
    /* Present the search-results: */
    print "<h2>[Squashy] Search Results For '".$_POST['keyword']."':</h2>\n";
    for( $i = 1; $row = mysql_fetch_array($result); $i++ )
    {
        print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
        print "(occurrences: ".$row['occurrences'].")<br><br>\n";
    }
    /* Present how long it took the execute the query: */
    print "This search took: ".(substr($end_time-$start_time,0,5))." seconds.";
}
else
{
    /* If no keyword is defined, present the search-page instead: */
    print "<form method='post'>[Squashy Search] <input type='text' size='20' name='keyword'>\n";
    print "Results: <select name='results'><option value='5'>5</option>\n";
    print "<option value='10'>10</option><option value='15'>15</option>\n";
    print "<option value='20'>20</option></select>\n";
    print "<input type='submit' value='Search [Squashy]'></form>\n";
}
print "</body></html>\n";
/* Simple function for retrieving the currenct timestamp in microseconds: */
function getmicrotime()
{
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
?>
I DO NOT WANT TO FOLLOW ANOTHER TUTORIAL PLEASE BECAUSE I AM A N00B!


Thanks Please help Soon!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Search Multiple Keywords Not one

Post by Jonah Bron »

Here's an example.

Code: Select all

$words = $_GET['query']; // the search box
$words = explode(' ', $words);
$words = 'some_column LIKE "%' . implode('" OR some_column LIKE "', $words) . '%"';
$query = 'SELECT * FROM some_table WHERE ' . $words;
// execute query
Do you see how that works? It breaks up the string at each space, and then puts it back together in the form of a query using LIKE.
willevans1
Forum Newbie
Posts: 4
Joined: Tue Sep 21, 2010 3:21 pm

Re: Search Multiple Keywords Not one

Post by willevans1 »

Please could you tell me where to put this in my code, or please could you make me the code :( I am inexperienced.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Search Multiple Keywords Not one

Post by Jonah Bron »

Actually, it's a bit different than I thought. I think this works.

Code: Select all

$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 (false 
';
$words = explode(' ', $_POST['keyword']);
foreach ($words as $word) {
    $query .= 'OR w.word_word = "' . $word . '" ';
}
$query .= ')
   GROUP BY
   p.page_id
   ORDER BY
   occurrences DESC
   LIMIT ' . $_POST['results']);

$query = mysql_query($query);
Put this right in the place of the query you have now.
willevans1
Forum Newbie
Posts: 4
Joined: Tue Sep 21, 2010 3:21 pm

Re: Search Multiple Keywords Not one

Post by willevans1 »

In the original code I have $result should it be that or $query please could you just double check the original code and the one you have sent me so I can make sure it works in the morning.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Search Multiple Keywords Not one

Post by Jonah Bron »

Oops, yeah.

Code: Select all

$result = mysql_query($query);
Post Reply