2016-03-19 40 views
0

私は、ストリッピング、いくつかのPHPの文字列の結果をSQLクエリの問題を抱えている:完全に一致SQL

私は部分的に郵便番号に基づいてフィールドを照会しています。これは、 'HS5'との部分一致として検索語 'S5'が一致しているため、失敗しています。

検索語句は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%'"); 
+0

わからない、しかし、あなたは全体のワードマッチ '$クエリを探している可能性があり= ($ left、$ right)= explode( '$'); $ db-> query( "SELECT * FROM shipping_rules where country_string REGEXP '[[:<:]] $国[[:>:]]'"); ' –

+0

' '、$ customer_postcode);' –

答えて

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'"); 
+0

申し訳ありません - $ islands_postcodesはSQLの 'country_string'の単一の値であることを忘れています –

+0

はい、 – C2486

1

のようにしてください使用しないよう%を削除することができますコード内のいくつかのエラー。

とにかく、あなたが行う場合:どのように計算されたの$ customer_postcodeの結果として

$country = 'S5' 

$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); 

は、あなたはそれを参照してください。

あなたは$ islands_postcodesから完全な郵便番号を抽出するために異なる戦略を設計するために持っているか、単に実行します。

$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 = 'H'.$customer_postcode; 

print($country); 

よろしく

関連する問題