我有一个用变量动态构造的查询。我希望我的所有条件也有类似的声明。
我的代码是:
$where .= " AND name=" . "'" . $name . "'";
我想用:
$where .= " AND name Like "%'.$name.'%";
但我不确定如何构建它。
由于
答案 0 :(得分:1)
看起来你走在正确的轨道上。像这样......
$where .= " AND name LIKE '%" . $name . "%'";
但是,在向字符串添加字段时,我发现更容易使用php sprintf()函数。举个例子:
$where = sprintf("SELECT * FROM Users WHERE name LIKE '%s' AND status='%s'",
"%" . mysql_real_escape_string($name) . "%",
"active"
);
答案 1 :(得分:0)
你可以简单地做
$where .= " AND name Like '%".$name."'%";
答案 2 :(得分:0)
$where .= " AND name Like '%$name%'";
或
$where .= ' AND name Like "%'.$name.'%"';
答案 3 :(得分:0)
像这样:
$where .= " AND name LIKE '%".mysql_real_escape_string($name)."%'";