Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003
    Posts
    145
    Plugin Contributions
    0

    Default Search SQL - Suggest: lower() + strtolower()

    I'm using PHP5 & MySQL5 and according to the MySQL docs, the LIKE comparator is case-sensitive on strings. Seeing as the zen_parse_search_string function drops everything to lowercase, the advanced_search_result/header_php.php should have LOWER() modifiers put in around lines 282-326:
    Code:
    if (isset($keywords) && zen_not_null($keywords)) {
      if (zen_parse_search_string(stripslashes($_GET['keyword']), $search_keywords)) {
        $where_str .= " AND (";
        for ($i=0, $n=sizeof($search_keywords); $i<$n; $i++ ) {
          switch ($search_keywords[$i]) {
            case '(':
            case ')':
            case 'and':
            case 'or':
            $where_str .= " " . $search_keywords[$i] . " ";
            break;
            default:
            $where_str .= "(LOWER(pd.products_name) LIKE '%:keywords%'
                                             OR LOWER(p.products_model)
                                             LIKE '%:keywords%'
                                             OR LOWER(m.manufacturers_name)
                                             LIKE '%:keywords%'";
    
            $where_str = $db->bindVars($where_str, ':keywords', $search_keywords[$i], 'noquotestring');
            // search meta tags
            $where_str .= " OR (LOWER(mtpd.metatags_keywords)
                            LIKE '%:keywords%'
                            AND mtpd.metatags_keywords !='')";
    
            $where_str = $db->bindVars($where_str, ':keywords', $search_keywords[$i], 'noquotestring');
    
            $where_str .= " OR (LOWER(mtpd.metatags_description)
                            LIKE '%:keywords%'
                            AND mtpd.metatags_description !='')";
    
            $where_str = $db->bindVars($where_str, ':keywords', $search_keywords[$i], 'noquotestring');
    
            if (isset($_GET['search_in_description']) && ($_GET['search_in_description'] == '1')) {
              $where_str .= " OR LOWER(pd.products_description)
                              LIKE '%:keywords%'";
    
              $where_str = $db->bindVars($where_str, ':keywords', $search_keywords[$i], 'noquotestring');
            }
            $where_str .= ')';
            break;
          }
        }
        $where_str .= " )";
      }
    }
    HTH, Thanks.

  2. #2
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,896
    Blog Entries
    2
    Plugin Contributions
    2

    Default Re: Search SQL fix

    Hi,

    Will log this as a bug. I agree with that LIKE and its other incarnations are case sensitive. Will need to check other code as well wher we use LIKE in a query.

    Thanks for the report

  3. #3
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Search SQL fix

    While I understand the theory presented, I can't create the problem described. Testing on MySQL 4.1.14.

    I was thinking this is only a problem if the collation method set on the database is set to a binary collation (ie: latin1_bin) or case-sensitive (ie: latin1_general_cs) method instead of a case-insensitive (ie: latin1_general_ci) collation method.
    But, when I set up a new database using latin1_general_cs as collation method, anticipating case-sensitive behaviour, the searches still work fine. Same when choosing latin1_bin (binary), as well as latin1_general_ci and latin1_swedish_ci (which are case-insensitive collation methods).

    From the MySQL manual:
    Normally, if any expression in a string comparison is case sensitive, the comparison is performed in case-sensitive fashion.
    Since the syntax of the LIKE comparison is "expr LIKE pattern", it would seem that "expr" is the database-content portion (case not converted), and "pattern" is the pattern we're matching against, which is pre-converted to lowercase.

    It would seem at the present time that MySQL4 (well 4.1 anyway) doesn't suffer from the case-sensitive issue you mention. Either that or it has nothing to do with collation.

    Notwithstanding that the theory you present is logical, I can't create the problem described in order to confirm that the suggested fix is truly the best approach.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Product description strtolower
    By crixus in forum General Questions
    Replies: 7
    Last Post: 5 Nov 2013, 01:00 PM
  2. Search - suggest alternate word?
    By compnet32 in forum General Questions
    Replies: 0
    Last Post: 8 Oct 2007, 02:00 PM
  3. ucwords & strtolower
    By Revenant in forum General Questions
    Replies: 0
    Last Post: 29 Jan 2007, 08:04 PM
  4. Upgrade to 1.3.0.5 - strtolower question
    By icemanchai in forum Upgrading from 1.3.x to 1.3.9
    Replies: 2
    Last Post: 4 Sep 2006, 11:45 AM

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
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR