3

我正在尝试在 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%'";
4

3 回答 3

6

通配符在错误的位置:

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

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

于 2013-01-05T08:23:50.083 回答
3

你应该移动百分号。

AND pros_num LIKE '$phone%'";

变成

AND pros_num LIKE '%$phone'";
于 2013-01-05T08:23:57.743 回答
1
$s_phone = " AND pros_num LIKE '%{$phone}%'";

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

或者:

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

% 符号是通配符!

于 2013-01-05T08:25:51.977 回答