SQL code question

Discussion in 'PHP' started by qwikad.com, Jul 25, 2024.

  1. #1
    Been working on a little search option. When I enter a single word into the search it brings up all the results related to the word. However, with two or more word phrases the search fails to bring up results. The SQL code looks fine to me:

    (a.posttitle LIKE '%$keywords%' OR a.post LIKE '%$keywords%')

    What could be the problem? The spaces between the words?

    Maybe there's another way of doing this (instead of LIKE %%)?
     
    qwikad.com, Jul 25, 2024 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,790
    Likes Received:
    4,529
    Best Answers:
    123
    Trophy Points:
    665
    #2
    do something like this (untested, typed quickly)

    
    $bits = explode(' ', $keywords)
    
    $where = [];
    foreach($bits as $bit){
       $where[] = ' a.posttitle LIKE '%bit%';
       $where[] = ' a.post LIKE '%bit%';
    }
    
    $sql = "select a.id, a.posttitle, a.post from a where a.status = 'published' and (". implode(' OR ', $where) . ') order by a.posttitle';
    
    Code (php):
     
    sarahk, Jul 25, 2024 IP
    qwikad.com likes this.
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,265
    Likes Received:
    1,693
    Best Answers:
    31
    Trophy Points:
    475
    #3
    Thank you. I had to change some things, but, yes, that's what I needed.
     
    qwikad.com, Jul 26, 2024 IP