完全匹配SQL

时间:2016-03-19 12:03:06

标签: php mysql regex

我的SQL查询问题与一些PHP字符串剥离的结果有关:

我正在查询基于部分邮政编码的字段。这是失败的,因为它匹配搜索条件' S5'作为' HS5'的部分匹配。

搜索词可以是S5或HS5,但我只想匹配HS5。

我的代码如下所示:

$customer_postcode ='S5 9PO';
//strip anything after the space in a postcode
$customer_postcode = substr($customer_order_postcode, 0, strrpos($customer_order_postcode, ' '));


$islands_postcodes = 'HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42,IV43,IV44,IV45,IV46,IV48,V49,IV51,IV55,IV56,KA27,KA28,KW15,KW16,KW17,PA20,PA41,PA42,PA43,PA44,PA45,PA46,PA47,PA48,PA49,PA60,PA61,PA62,PA63,PA64,PA65,PA66,PA67,PA68,PA69,PA70,PA71,PA72,PA73,PA74,PA75,PA76,PA77,PA78,PH41,PH42,PH43,PF44,ZE1,ZE2,ZE3,IM,GY,JE';

$search_highlands = strpos($highland_postcodes, $customer_postcode);

if($search_highlands==true) $country = $customer_postcode;

$query = $db->query("SELECT * FROM shipping_rules WHERE country_string LIKE '%$country%'");

2 个答案:

答案 0 :(得分:1)

请注意,代码中存在许多错误。

无论如何,如果你这样做:

<?php

    $src    = '2fuse.jpg';
    $im     = imagecreatefromjpeg($src);
    $size   = getimagesize($src);
    $width  = $size[0];
    $height = $size[1];

    for($x=0;$x<$width;$x++)
    {
        for($y=0;$y<$height;$y++)
        {
            $rgb = imagecolorat($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            var_dump($r, $g, $b);
        }
    }
你看到了:

$customer_order_postcode ='S5 9PO';
//strip anything after the space in a postcode
$customer_postcode = substr($customer_order_postcode, 0, strrpos($customer_order_postcode, ' '));

$islands_postcodes = 'HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IV41,IV42,IV43,IV44,IV45,IV46,IV48,V49,IV51,IV55,IV56,KA27,KA28,KW15,KW16,KW17,PA20,PA41,PA42,PA43,PA44,PA45,PA46,PA47,PA48,PA49,PA60,PA61,PA62,PA63,PA64,PA65,PA66,PA67,PA68,PA69,PA70,PA71,PA72,PA73,PA74,PA75,PA76,PA77,PA78,PH41,PH42,PH43,PF44,ZE1,ZE2,ZE3,IM,GY,JE';

$search_highlands = strpos($islands_postcodes, $customer_postcode);

if($search_highlands==true) $country = $customer_postcode;

print($country);

是您计算$ customer_postcode的结果。

你必须设计一个不同的策略来从$ islands_postcodes中提取完整的邮政编码,或者只是这样做:

$country = 'S5'

此致

答案 1 :(得分:0)

您可以像这样删除%

$query = $db->query("SELECT * FROM shipping_rules WHERE country_string LIKE '$country'");

或者不要使用like尝试这样

$query = $db->query("SELECT * FROM shipping_rules WHERE country_string= '$country'");