SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Searching my 1 table database

    Sorry for the following non-normalised database, but i am a little desperate so this is the easiest way.

    1 db- 1tbl

    Now I want to search my entire db on $searchword
    why doesn't the following sql-query work:


    $sql = "SELECT * FROM tbl WHERE name LIKE '%$searchword%' AND city LIKE '%$searchword%' ORDER BY name OR city DESC LIMIT 0, 25";

    Grtz

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens when you run this through? Does it just give you an empty set result?

    Make sure it's not searching for "%$searchword%" and is actually parsing the variable.. To check, you can alter the string to

    $sql = "SELECT * FROM tbl WHERE name LIKE '%".$searchword."%' AND city LIKE '%".$searchword."%' ORDER BY name OR city DESC LIMIT 0, 25";

    Otherwise I'm not really sure. Have you tried to run the query while logged into mysql? Was it successful?

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW what error are you getting? I think its because of your ORDER BY clause, you can't use OR in it. Try
    Code:
    $sql = "SELECT * FROM tbl WHERE name LIKE '%".$searchword."%' AND city LIKE '%".$searchword."%' ORDER BY name, city DESC LIMIT 0, 25";
    Also does the searchword HAVE to appear in both fields or is it either/or or both If thats the case you need to change the AND to a OR
    Code:
    $sql = "SELECT * FROM tbl WHERE name LIKE '%".$searchword."%' OR city LIKE '%".$searchword."%' ORDER BY name, city DESC LIMIT 0, 25";
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •