如何在mysql中使用长字符串值

时间:2013-01-05 08:17:40

标签: php mysql sql-like phone-number

我正在尝试在mysql中使用LIKE语句

我想要搜索的值是

例如:

(999) 999-9999
(999) 888-8888
(888) 888-8888
(888) 999-9999

当我试图搜索

(888) or (999)

它显示得很好,但是当我在寻找

999-9999 or 888-8888

值不会显示

数据库的php脚本:

if($area != "" & $phone != ""){

         $phone = $area.' '.$phone;
         $s_phone = " AND pros_num ='$phone'";

}else if($area == "" & $phone != "")

         $s_phone = " AND pros_num LIKE '$phone%'";
else

         $s_phone = " AND pros_num LIKE '$area%'";

3 个答案:

答案 0 :(得分:6)

通配符位于错误的位置:

$s_phone = " AND pros_num LIKE '%$phone'";

[编辑]
http://www.tizag.com/mysqlTutorial/mysqlwhere.php将提供有关通配符使用的指导。

答案 1 :(得分:3)

您应该移动百分号。

AND pros_num LIKE '$phone%'";

变为

AND pros_num LIKE '%$phone'";

答案 2 :(得分:1)

$s_phone = " AND pros_num LIKE '%{$phone}%'";

将搜索所需的字符串。是否在它之前或之后有一些字符。

或者:

$s_phone = " AND pros_num LIKE '%{$phone}'";

%sign是一个通配符!