SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast dave_merwin's Avatar
    Join Date
    Apr 2003
    Location
    Eugene, OR
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SELECT LIKE issue for site search

    Anyone have any idea what the following select will not work. I am accepting the variable $search from a search form and the doing a select with that variable. Cept, it returns nothing. I really like the my phpmyadmin runs a search, but not sure how ot do that.

    Anyway, here is the code. Please let me know what you think.

    By the way, why aren't I using FULLTEXT? My users search for things like cd and I am on MySQL 3.23 on a virtual box. Upgrade and configuration not possible.

    Thanks

    PHP Code:
    $result $db->query ('SELECT products.name, products.description_short, products.map, products.id AS pid 
    FROM products 
    WHERE products.name LIKE %'
    .$search.'% 
    OR products.description_short LIKE "%'
    .$search.'%" 
    OR products.description_long LIKE "%'
    .$search.'%" 
    OR brands.name LIKE %'
    .$search.'% 
    OR category.name LIKE "%'
    .$search.'%" 
    OR deals.title LIKE "%'
    .$search.'%" 
    OR deals.description LIKE "%'
    .$search.'%" 
    OR reviews.title LIKE "%'
    .$search.'%" 
    OR reviews.review LIKE "%'
    .$search.'%"'); 
    Dave Merwin

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My advice here is to generate the sql in a separate string variable then echo it before you execute the query.

    You have problems with your use of quotes around the $search variable. On the where products.name the % seems to be outside the quote. Or not in a quote at all.

    On the next line you have quotes. There is inconsistancy there which needs looking at.

    A point to note with php strings ... if you use the double quote you don't need to close the quote when using a variable. For example, the following two lines will produce the same output:

    PHP Code:
    echo 'tumtitum' $variable 'blahblah' ;
    echo 
    "tumtitum $variable blahblah" 
    Also note that if you want ot include a double quote inside a double quoted string you can escape it with a \. For example:

    PHP Code:
     $str "Mike says \"Hi\" "
    Would produce the output Mike says "Hi"

    I hope this helps,

    Mike


Bookmarks

Posting Permissions

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