1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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:
    29,001
    Likes Received:
    4,577
    Best Answers:
    124
    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,411
    Likes Received:
    1,735
    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