Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    97
    Thanks
    2
    Thanked 0 Times in 0 Posts

    mysql search query

    Hello, I created a basic search query using %nametosearch% but for example if I'm searching for "Javascript web forum" and I input "Javascript forum" it doesn't work, how can I improve it ?

    Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,685
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    But if you aren't willing to move to full text search, then just use a little bit of logic!

    Code:
    <?php
    
    $search = explode( " ", $_POST["search"] );
    $sql = "SELECT * FROM sometable ";
    $delimiter = " WHERE ";
    foreach ( $search as $word )
    {
        if ( $word != "" )
        {
            $sql .= $delimiter . "somefield LIKE '%" . $word . "%' ";
            $delimiter = " AND ";
        }
    }
    ... then execute that $sql query ...
    So if the user enters "javascript forum" in the <form> page, that will create a query the looks like
    Code:
    SELECT * FROM sometable WHERE somefield LIKE '%javascript%' AND somefield LIKE '%forum%'
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The above may also give some result for "java forum"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,685
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    Quote Originally Posted by abduraooft View Post
    The above may also give some result for "java forum"
    The "above" being my code?

    Not true.

    Look again:
    Code:
    SELECT * FROM sometable WHERE somefield LIKE '%javascript%' AND somefield LIKE '%forum%'
    If I had used OR in place of AND you would be correct.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •