三个下拉菜单如何实现php模糊查询,各位师兄看看下面的代码错在哪里。。谢了
php代码:$result=mysql_query("selectidfromkechengwheretitlelike'%$_POST[kecheng]%'orname...
php代码:$result = mysql_query("select id from kecheng where title like '%$_POST[kecheng]%' or name like '%$_POST[name]%' or city like '%$_POST[city]%' ");
$total = mysql_num_rows($result);
pageft($total, 3);
if ($firstcount < 0)
$firstcount = 0;
$query = $db->findall("kecheng where title like '%$_POST[kecheng]%' or name like '%$_POST[name]%' or city like '%$_POST[city]%' limit $firstcount, $displaypg");
while ($row = $db->fetch_array($query)) {
$sm_list[] = array (
"city"=>$row[city],
"name"=>$row[name],
"title" => $row[title],
"telephone" => $row[telephone],
"id" => $row[id],
"date_time" => date("m/d",$row[datetime]));
}
$smarty->assign("sm_list", $sm_list);
$smarty->assign("pagenav", $pagenav); 展开
$total = mysql_num_rows($result);
pageft($total, 3);
if ($firstcount < 0)
$firstcount = 0;
$query = $db->findall("kecheng where title like '%$_POST[kecheng]%' or name like '%$_POST[name]%' or city like '%$_POST[city]%' limit $firstcount, $displaypg");
while ($row = $db->fetch_array($query)) {
$sm_list[] = array (
"city"=>$row[city],
"name"=>$row[name],
"title" => $row[title],
"telephone" => $row[telephone],
"id" => $row[id],
"date_time" => date("m/d",$row[datetime]));
}
$smarty->assign("sm_list", $sm_list);
$smarty->assign("pagenav", $pagenav); 展开
2个回答
展开全部
你的查询语句查的是并集,难道kecheng,name,city不是同一个表的字段么?
当然,用你的查询语句也能查到记录,但是却不能做到精确定位一条记录
如一条记录:"city"=>"china" ,"name"=>"myname" , "title " => "English"
你的查询语句这时是:
"select id from kecheng where title like '%English%' or name like '%myname%' or city like '%china%' "
这时查到的记录,并非只有同时包含‘china’、‘myname’、‘English’,所以你的查询语句是失败的。
你应该改成这样:
$where = " where 1=1";//注意where前面有空格
if($_POST[kecheng])$where .= " AND title like '%$_POST[kecheng]%'";//注意AND前面有空格
if($_POST[name])$where .= " AND name like '%$_POST[name]%'";
if($_POST[city])$where .= " AND city like '%$_POST[city]%'";
$result = mysql_query("select id from kecheng ".$where);
同样下面的赋值给$query 变量的语句也依照此修改,就能实现模糊查询,并根据条件精确定位记录了。
当然,用你的查询语句也能查到记录,但是却不能做到精确定位一条记录
如一条记录:"city"=>"china" ,"name"=>"myname" , "title " => "English"
你的查询语句这时是:
"select id from kecheng where title like '%English%' or name like '%myname%' or city like '%china%' "
这时查到的记录,并非只有同时包含‘china’、‘myname’、‘English’,所以你的查询语句是失败的。
你应该改成这样:
$where = " where 1=1";//注意where前面有空格
if($_POST[kecheng])$where .= " AND title like '%$_POST[kecheng]%'";//注意AND前面有空格
if($_POST[name])$where .= " AND name like '%$_POST[name]%'";
if($_POST[city])$where .= " AND city like '%$_POST[city]%'";
$result = mysql_query("select id from kecheng ".$where);
同样下面的赋值给$query 变量的语句也依照此修改,就能实现模糊查询,并根据条件精确定位记录了。
追问
师兄,这个$query咋写呀。。。写了之后还是不能按照所选择的下拉菜单选择。。。。。。。。。。$query = $db->findall("kecheng ".$where." limit $firstcount, $displaypg");
这样写正确吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询