我有一个用变量动态构造的查询。我希望我的所有条件也有类似的声明。
我的代码是:
$where .= " AND name=" . "'" . $name . "'";
我想使用:
$where .= " AND name Like "%'.$name.'%";
但我不确定如何构建它。
谢谢
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"
);
你可以简单地做
$where .= " AND name Like '%".$name."'%";
$where .= " AND name Like '%$name%'";
或者
$where .= ' AND name Like "%'.$name.'%"';
像这样 :
$where .= " AND name LIKE '%".mysql_real_escape_string($name)."%'";