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 6 of 6
  1. #1
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts

    mysql select where question

    hello,
    i got another doubt,
    as i have told you before i have a directory of websites in different categories.
    when visitors search for some keyword, i have to search for it in titles, descriptions of websites.

    the sql query is:
    $sth = mysql_query("SELECT * FROM cb_urls WHERE title LIKE '%".addSlashes($keywords)."%' OR description LIKE '%".addSlashes($keywords)."%' ");

    this query seaches all websites in all categories.

    but i would like to search in only a particular category.
    i have stored that category in a string $category.

    now i want search results only in that particular category.
    how can i do it.
    thank you.

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    just add

    Code:
    where category ='".$category."'
    so it'd be

    Code:
    $sth = mysql_query("SELECT * FROM cb_urls WHERE title LIKE '%".addSlashes($keywords)."%' OR description LIKE '%".addSlashes($keywords)."%' WHERE category='".$category."'");

  • #3
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    thanks for a quick reply,
    can i use WHERE clause twice in the same query?
    thanks

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    yeah you can put an AND statement in...

    Code:
    WHERE category = '".$category."' AND somethingelse = '".$somethingelse."'
    (you can use OR as well there)
    eg:

    Code:
    WHERE category = '".$category1."' OR category = '".$category2."'

  • #5
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    i have to search for keywords in title and description only when category = $category.

    can i use this query?

    $sth= mysql_query("SELECT * FROM table WHERE category='".addSlashes($category)."' AND title LIKE '%".addSlashes($keywords)."%' OR category='".addSlashes($category)."' AND description LIKE '%".addSlashes($keywords)."%'");

    this code is working,
    but is it efficient? is there any better method of coding this?
    thanks

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That code would work but to achieve what you want you need to add brackets to the code as bellow

    PHP Code:
    <?php

    $sth
    mysql_query("SELECT * FROM table WHERE (category='".addSlashes($category)."' AND title LIKE '%".addSlashes($keywords)."%') OR (category='".addSlashes($category)."' AND description LIKE '%".addSlashes($keywords)."%')");
    ?>
    that way it will do either the first category=$category and title like blah OR category = $category and desciption like $keywords

    ... actually come to think of it the code should be

    PHP Code:
    <?php

    $sth
    mysql_query("SELECT * FROM table WHERE category='".addSlashes($category)."' AND (title LIKE '%".addSlashes($keywords)."%') OR  description LIKE '%".addSlashes($keywords)."%')");
    ?>
    That way you are saying category = $category but the keyword can be in title OR description
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)


  •  

    Posting Permissions

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