PHP sql拼接技巧

if ($keyword != ”)
$wheres[] = ” (t1.username like ‘%$keyword%’ Or t2.car_brand_txt like ‘%$keyword%’
Or t2.car_type_txt like ‘%$keyword%’ Or t2.shouru like ‘%$keyword%’ Or t2.zhiye like ‘%$keyword%’
Or t2.hunyin like ‘%$keyword%’ Or t2.sex like ‘%$keyword%’ Or t2.address like ‘%$keyword%’ Or t2.zip like ‘%$keyword%’
Or t2.car_no like ‘%$keyword%’ Or t2.member_no like ‘%$keyword%’) “;

$car_brand_id > 0 ? $wheres[] = ” t2.car_brand_id = ‘$car_brand_id’ ” :0;
$car_type_id > 0 ? $wheres[] = ” t2.car_type_id = ‘$car_type_id’ ” :0;
$shouru != ” ? $wheres[] = ” t2.shouru = ‘$shouru’ ” :0;
$zhiye != ” ? $wheres[] = ” t2.zhiye = ‘$zhiye’ ” :0;
$hunyin != ” ? $wheres[] = ” t2.hunyin = ‘$hunyin’ ” :0;
$sex != ” ? $wheres[] = ” t2.sex = ‘$sex’ ” :0;
$zip != ” ? $wheres[] = ” t2.zip like ‘%$zip%’ ” :0;
$car_no != ” ? $wheres[] = ” t2.car_no like ‘%$car_no%’ ” :0;
$address != ” ? $wheres[] = ” t2.address like ‘%$address%’ ” :0;
$member_no != ” ? $wheres[] = ” t2.member_no like ‘%$member_no%’ ” :0;

$whereSql = join(‘ And ‘,$wheres);
if($whereSql!=”)
{
$whereSql = ‘ where ‘.$whereSql;
}
$sql = “SELECT t1.uid,t1.username,t1.regdate,t2.* From cdb_members AS t1 LEFT JOIN cdb_memberfields AS t2 ON t1.uid=t2.uid
$whereSql
ORDER BY t1.uid desc “;



发表评论

电子邮件地址不会被公开。 必填项已用*标注

51 − = 48