Php like query

Discussion in 'PHP' started by le007, Aug 26, 2008.

  1. #1
    Hi all,

    I'm working on making a search box for my database. Can someone please elaborate it a little. I need to break up the text that is in 'search' by spaces so for example: the words searched were "book with words" and the keywords field in the database only had "book words" in it - I need to be able to search for more than the exact phrase.

    I trying to get it right but its tricky. I need to manually enter keywords, a link and a description to my db. 3 fields. i then want a text box where people can use keywords to search and the corresponding link and description will appear. So far, below is what I'm working at but it ain't working properly.
    Thanks,

    Le007



    
    
    
    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'password';
    
    $conn = mysql_connect($keywords, $link, $description) or die                      ('Error connecting to mysql');
    
    $searchy=$_REQUEST['search'];
    
    $result = mysql_query("SELECT * FROM dbase WHERE tags LIKE '%$searchy%' ") or die(mysql_error());
    
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
    foreach ($row as $key => $value) {$$key = $value;}
    // Print out the contents of each row
    echo "<a href='$url'>$details</a><br />";
    }
    Code (markup):

     
    le007, Aug 26, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Well i don't gonna write it all, i'm going to give you hints to functions on php and will describe in little what the use is.

    explode - split strings with given variabel (like space) returning an array.
    for-next to loop the array
    implode for converting the array to a string using for example ','
     
    EricBruggema, Aug 26, 2008 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thanks EricBruggema - I've come across code that is similar to what I need, I just need a hand putting it all together - any help is super.

    <?php
    $search_string = 'stuff and books and things'; // assume this is what they put into the form
    $words = explode(' ', $search_string); // will contain the array [0] => 'stuff', [1] => 'and', [2] => 'books', [3] => 'and', [4] => 'things'
    ?>
    
    <?php
    $silly_words = array('and', 'the', 'a', 'things');
    $real_words = array_diff($words, $silly_words); // will contain the array [0] => 'stuff', [2] => 'books'
    ?>
    
    <?php
    $likes = array();
    foreach ($real_words AS $search_term)
    {
      $likes[] = "tags LIKE '%$search_term%'";
    }
    ?>
    
    <?php
    $search_statement = implode(' AND ', $likes); // will contain "tags LIKE '%stuff%' AND tags LIKE '%books%'"
    ?>
    Code (markup):
     
    le007, Aug 26, 2008 IP