-2

我有一个用变量动态构造的查询。我希望我的所有条件也有类似的声明。

我的代码是:

$where .= " AND name=" . "'" . $name . "'";

我想使用:

$where .= " AND name Like "%'.$name.'%";

但我不确定如何构建它。

谢谢

4

4 回答 4

1

It's looks like you are on the right track. Like this...

$where .= " AND name LIKE '%" . $name . "%'";

But, when adding fields to a string, I find is easier to use the php sprintf() function. As an example:

$where = sprintf("SELECT * FROM Users WHERE name LIKE '%s' AND status='%s'",
    "%" . mysql_real_escape_string($name) . "%",
    "active"
);
于 2013-04-05T18:13:27.010 回答
0

你可以简单地做

$where .= " AND name Like '%".$name."'%";
于 2013-04-05T18:04:48.187 回答
0
$where .= " AND name Like '%$name%'";

或者

$where .= ' AND name Like "%'.$name.'%"';
于 2013-04-05T18:04:56.363 回答
0

像这样 :

$where .= " AND name LIKE '%".mysql_real_escape_string($name)."%'";
于 2013-04-05T18:05:07.240 回答