代码:
$nerd_result = mysql_query("select * from nerd_profile where nerd_reg_no = '$reg_no'");
$nerd_data = mysql_fetch_array($nerd_result);
$tags = array();
$tags = explode(",",$nerd_data['nerd_interests']);
for($i = 0; $i < sizeof($tags)-1; $i++)
{
if($i != sizeof($tags)-2)
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% or ";
}
else
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% ";
}
}
$proper_query = "select * from `qas_posts` where ".$sub_query." and `post_date` like '%$today%'";
$result = mysql_query($proper_query);
while($each_qas = mysql_fetch_array($result))
说明:
我在字符串中添加了like子句和php变量,并将其与带有like子句的其他变量连接起来。最后当我回声时,我得到了我想要的完美查询,但
mysql_fetch_array()
不接受生成的查询,而不是我硬编码,它的工作完美我做错了什么?我可以这样做吗?
答案 0 :(得分:2)
在mysql中进行字符串比较时,您需要确保在比较值周围有引号。
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' or ";
和
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' ";