Results 1 to 6 of 6
  1. #1
    Full Member suzie250's Avatar
    Join Date
    May 5th, 2005
    Posts
    416
    LIKE '%$categoryid%' works = doesn't in select statement
    I can get the first query to work fine. I don't understand why I can't get it to work if I write it like the second one.

    $query = "SELECT * FROM categorylist WHERE catid LIKE '%$categoryid%'";

    $query = 'SELECT * FROM categorylist WHERE catid=\" '.$categoryid.'\" ';

    Can someone give me a clue?
    Thanks,
    Suzie
    I disagree with Kay Jewelers. I would bet on any given Friday or Saturday night more kisses begin with Bud Lite than Kay.

  2. #2
    Newbie
    Join Date
    December 5th, 2005
    Location
    BC, Canada
    Posts
    5
    If you haven't already figured this out,

    $query = 'SELECT * FROM categorylist WHERE catid=\" '.$categoryid.'\" ';

    should just be

    $query = 'SELECT * FROM categorylist WHERE catid=" '.$categoryid.'"';

    you only need to \" if you enclosed the entire string in " " but you used ' '

    $query = "SELECT * FROM categorylist WHERE catid=\"".$categoryid."\"";

    thats when you would use \

  3. #3
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,232
    If you're using Perl and the DBI module, it's best to do something like this:

    $query = "SELECT * FROM categorylist WHERE catid = ?";
    $sth = $dbh->prepare($query);
    $sth->execute($categoryid);

    The DBI module will handle quoting the strings and everything else that can throw things off.
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  4. #4
    ABW Ambassador FFoc's Avatar
    Join Date
    January 18th, 2005
    Posts
    1,015
    ...And similarly with the PEAR "DB" module

  5. #5
    pph Expert! Gordon's Avatar
    Join Date
    January 18th, 2005
    Location
    Edmonton Canada
    Posts
    5,781
    SELECT DISTINCT productgroup, secondarycat, maincat FROM database WHERE 1 AND maincat LIKE '%Womens%' AND brandname = 'brand'
    I have used the query above and it works fine but when I try to use the one below it fetches up both mens and womens.
    SELECT DISTINCT productgroup, secondarycat, maincat FROM database WHERE 1 AND maincat LIKE '%Mens%' AND brandname = 'brand'
    I have tried to leave a gap like maincat AND NOT LIKE '% mens%' and maincat AND NOT LIKE '% mens %' but it does not work at all could somebody please let me know what I am doing wrong?

    Thanks in advance
    One day parasites and their ilk will be made illegal, I bet a few Lawyers will be pissed off when the day comes.
    Mr. Spitzer is fetching it nearer

    YouTrek

  6. #6
    Resident Genius and Staunch Capitalist Leader's Avatar
    Join Date
    January 18th, 2005
    Location
    Florida
    Posts
    12,817
    The % character is a wildcard. So, that second one is telling it to bring up everything with the character sequence "mens" in it, regardless of the characters on either side. So, of course, "womens" comes up too, since the wildcard on both ends is allowing any characters that are around the "mens" sequence.

    Try adding AND NOT LIKE '%womens%' to it to exclude those matches.
    There is no knowledge that is not power. ~Hemingway

  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Using an array from a mysql field in a select statement
    By knight01 in forum Programming / Datafeeds / Tools
    Replies: 9
    Last Post: May 9th, 2010, 10:16 PM
  2. show select products on index page with IF statement
    By sweetiowa in forum WebMerge (Fourthworld.com)
    Replies: 3
    Last Post: October 13th, 2007, 08:30 PM
  3. Help with PHP Select statement
    By Doug247 in forum Programming / Datafeeds / Tools
    Replies: 1
    Last Post: July 30th, 2004, 12:01 PM
  4. using contain statement for "Men's"
    By aprillougheed in forum WebMerge (Fourthworld.com)
    Replies: 1
    Last Post: May 27th, 2004, 08:30 AM
  5. "IF" statement hiccup?
    By Haiko de Poel, Jr. in forum Cusimano.com Scripts
    Replies: 2
    Last Post: February 13th, 2003, 03:41 PM

Posting Permissions

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